@ladybugs/mcp
v0.0.1
Published
Model Context Protocol server for LadyBugs MongoDB trace database
Maintainers
Readme
@ladybugs/mcp
Model Context Protocol (MCP) server for querying the LadyBugs MongoDB trace database. Provides AI assistants with read-only access to execution traces for analysis and debugging.
Overview
This MCP server enables AI assistants (like Claude) to query and analyze execution traces stored in MongoDB. It provides two powerful tools with built-in safety restrictions to prevent data modification.
Features
- Read-only access: No ability to modify or delete traces
- Document limit enforcement: Maximum 1000 documents per query (configurable by AI, capped at 1000)
- Two query methods: Simple
findqueries and complexaggregatepipelines - Rich trace data: Function calls, variables, loops, errors, and execution flow
Installation
# From workspace root
pnpm install
# Build the package
cd packages/mcp
pnpm run buildUsage
Running the MCP Server
# Start the server
pnpm run start
# Or with development watch mode
pnpm run devConfiguring Claude Desktop
Add to your Claude Desktop MCP configuration (claude_desktop_config.json):
Option 1: Using NPX (Recommended - uses published package)
{
"mcpServers": {
"ladybugs": {
"command": "npx",
"args": ["-y", "@ladybugs/mcp"],
"env": {
"LADYBUGS_SERVER_URL": "http://localhost:3000"
}
}
}
}Option 2: Local Development (uses local build)
{
"mcpServers": {
"ladybugs": {
"type": "stdio",
"command": "node",
"args": [
"/path/to/your/Workspace/packages/mcp/build/index.js"
],
"env": {
"LADYBUGS_SERVER_URL": "http://localhost:3000"
}
}
}
}Environment Variables
LADYBUGS_SERVER_URL- URL of the LadyBugs server (default:http://localhost:3000)- This is where the MCP server will send queries to access the MongoDB database
- Change this if your LadyBugs server is running on a different host or port
Database Schema
Collection: traces
Each trace document has the following structure:
{
rawId: string, // Unique trace identifier
parentId: string, // Parent trace ID (for building execution tree)
children: string[], // Array of child trace IDs
rawTrace: {
id: string,
action: string, // Trace type (see below)
timestamp: number, // Milliseconds since epoch
// Action-specific fields...
}
}Trace Action Types
The rawTrace.action field determines the trace type:
Function Traces
functionCall: Function executionfunctionName: Name of the functionparams: Array of parameter valuesresult: Return value (if available)error: Error object (if function threw)
Variable Traces
variableDeclaration: Variable declaration/initializationvariableName: Name of the variablevalue: Initial valuekind: "const", "let", or "var"
variableAssignment: Variable assignment after declarationvariableName: Name of the variablevalue: New value
Loop Traces
forOfLoop: For-of loop iterationvariableName: Iterator variable namevalue: Current iteration valueiterable: Array/iterable being looped over
forInLoop: For-in loop iteration- Similar structure to forOfLoop
forLoop: Traditional for loop- Loop initialization, condition, and iteration details
Other Traces
templateLiteral: Template string evaluationexpressions: Array of interpolated expressionsresult: Final string value
dataFlow: Data access and manipulation- Tracks object property access and mutations
argumentFlow: Function argument tracking- Parameter passing and argument values
Available Tools
1. traces_mongodb_find
Simple query tool for finding traces by criteria.
Parameters:
query(required): MongoDB query filter objectlimit(optional): Max documents to return (default: 100, max: 1000)projection(optional): Fields to include/excludesort(optional): Sort specification
Example Queries:
// Find all function calls
{ "rawTrace.action": "functionCall" }
// Find specific function
{ "rawTrace.functionName": "calculateTotal" }
// Find traces with errors
{ "rawTrace.error": { "$exists": true } }
// Find by parent ID (get all children of a trace)
{ "parentId": "some-trace-id" }
// Find recent traces
{ "rawTrace.timestamp": { "$gte": 1704067200000 } }
// Complex query: Functions with errors in last hour
{
"rawTrace.action": "functionCall",
"rawTrace.error": { "$exists": true },
"rawTrace.timestamp": { "$gte": Date.now() - 3600000 }
}2. traces_mongodb_aggregate
Advanced aggregation pipeline for complex analysis.
Parameters:
pipeline(required): Array of aggregation stageslimit(optional): Max documents to return (default: 100, max: 1000)
Allowed Pipeline Stages:
$match: Filter documents$group: Group and aggregate data$sort: Sort documents$project: Reshape/select fields$limit: Limit results (automatically enforced)$skip: Skip documents$unwind: Deconstruct arrays$lookup: Join collections (read-only)$count: Count documents$facet: Multiple parallel aggregations
Blocked Stages (Read-only Protection):
$out: Blocked (would write to collection)$merge: Blocked (would modify collection)
Example Pipelines:
// Count traces by action type
[
{
"$group": {
"_id": "$rawTrace.action",
"count": { "$sum": 1 }
}
},
{
"$sort": { "count": -1 }
}
]
// Find most-called functions
[
{
"$match": {
"rawTrace.action": "functionCall"
}
},
{
"$group": {
"_id": "$rawTrace.functionName",
"callCount": { "$sum": 1 },
"avgDuration": { "$avg": "$rawTrace.duration" }
}
},
{
"$sort": { "callCount": -1 }
},
{
"$limit": 10
}
]
// Analyze error patterns
[
{
"$match": {
"rawTrace.error": { "$exists": true }
}
},
{
"$group": {
"_id": "$rawTrace.error.message",
"count": { "$sum": 1 },
"functions": { "$addToSet": "$rawTrace.functionName" }
}
}
]
// Build execution tree (parent-child analysis)
[
{
"$match": {
"parentId": "root-trace-id"
}
},
{
"$lookup": {
"from": "traces",
"localField": "rawId",
"foreignField": "parentId",
"as": "childTraces"
}
},
{
"$project": {
"functionName": "$rawTrace.functionName",
"childCount": { "$size": "$childTraces" }
}
}
]Safety Features
1. Read-Only Access
- No write operations allowed (
$out,$mergeblocked) - Only query and aggregation operations permitted
- Cannot modify or delete any trace data
2. Document Limit Enforcement
- Hard cap of 1000 documents per query
- AI can request lower limits, but cannot exceed 1000
- Automatic
$limitinjection in aggregations if not present - Prevents overwhelming responses and resource exhaustion
3. Query Validation
- Aggregation pipelines scanned for write operations
- Invalid stages rejected with clear error messages
- Connection state validated before queries
Common Use Cases
Debugging Function Errors
// Find tool
{
query: {
"rawTrace.action": "functionCall",
"rawTrace.functionName": "processPayment",
"rawTrace.error": { "$exists": true }
},
limit: 50
}Performance Analysis
// Aggregate tool
{
pipeline: [
{ "$match": { "rawTrace.action": "functionCall" } },
{
"$group": {
"_id": "$rawTrace.functionName",
"totalCalls": { "$sum": 1 },
"withErrors": {
"$sum": { "$cond": [{ "$ifNull": ["$rawTrace.error", false] }, 1, 0] }
}
}
},
{ "$sort": { "totalCalls": -1 } }
]
}Execution Flow Tracing
// Find all children of a function call
{
query: { "parentId": "function-trace-id" },
sort: { "rawTrace.timestamp": 1 },
limit: 200
}Variable Value Tracking
// Find all assignments to a variable
{
query: {
"rawTrace.action": { "$in": ["variableDeclaration", "variableAssignment"] },
"rawTrace.variableName": "userBalance"
},
sort: { "rawTrace.timestamp": 1 }
}Response Format
All tool responses follow this structure:
{
"success": true,
"count": 42,
"limit": 100,
"documents": [...]
}Error responses:
{
"success": false,
"error": "Error message here"
}Development
# Install dependencies
pnpm install
# Build
pnpm run build
# Watch mode for development
pnpm run dev
# Type checking
pnpm run typecheckArchitecture
The MCP server:
- Connects to the same MongoDB instance as the LadyBugs server
- Provides read-only query access via MCP protocol
- Enforces safety limits and validates queries
- Returns structured JSON responses to AI assistants
Dependencies
@modelcontextprotocol/sdk: MCP protocol implementation@ladybugs/server: Shares MongoDB connection and trace database access- MongoDB: Database for trace storage
License
MIT
