@musashishao/agent-kit 1.9.0 → 1.9.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.agent/agents/ai-asset-factory.md +700 -0
- package/.agent/agents/ai-audio-factory.md +503 -0
- package/.agent/agents/game-developer.md +4 -4
- package/.agent/agents/orchestrator.md +113 -3
- package/.agent/agents/project-planner.md +67 -0
- package/.agent/agents/unity-mobile-master.md +949 -0
- package/.agent/mcp/config/registry.json +65 -51
- package/.agent/mcp/servers/notebooklm/README.md +114 -0
- package/.agent/mcp/servers/notebooklm/package.json +35 -0
- package/.agent/mcp/servers/notebooklm/src/auth/chrome.ts +225 -0
- package/.agent/mcp/servers/notebooklm/src/auth/index.ts +1 -0
- package/.agent/mcp/servers/notebooklm/src/index.ts +516 -0
- package/.agent/mcp/servers/notebooklm/src/services/index.ts +3 -0
- package/.agent/mcp/servers/notebooklm/src/services/library.ts +217 -0
- package/.agent/mcp/servers/notebooklm/src/services/notebooklm.ts +380 -0
- package/.agent/mcp/servers/notebooklm/tsconfig.json +15 -0
- package/.agent/mcp-gateway/README.md +169 -20
- package/.agent/mcp-gateway/package.json +22 -7
- package/.agent/mcp-gateway/src/auth/index.ts +55 -0
- package/.agent/mcp-gateway/src/auth/middleware.ts +242 -0
- package/.agent/mcp-gateway/src/auth/oauth.ts +462 -0
- package/.agent/mcp-gateway/src/auth/scopes.ts +227 -0
- package/.agent/mcp-gateway/src/index.ts +252 -105
- package/.agent/mcp-gateway/src/observability/index.ts +5 -0
- package/.agent/mcp-gateway/src/observability/otel.ts +405 -0
- package/.agent/mcp-gateway/src/transports/index.ts +5 -0
- package/.agent/mcp-gateway/src/transports/streamableHttp.ts +235 -0
- package/.agent/rules/CODEX.md +89 -0
- package/.agent/rules/CODE_RULES.md +73 -0
- package/.agent/rules/GEMINI.md +25 -0
- package/.agent/rules/MEMORY_STATE.md +110 -0
- package/.agent/rules/REFERENCE.md +33 -141
- package/.agent/rules/REF_SKILLS.md +116 -0
- package/.agent/rules/REF_WORKFLOWS.md +81 -0
- package/.agent/scripts/ak_cli.py +106 -5
- package/.agent/scripts/memory_manager.py +48 -9
- package/.agent/skills/anti-hallucination/SKILL.md +295 -0
- package/.agent/skills/anti-hallucination/scripts/check_hallucination.py +299 -0
- package/.agent/skills/bifurcation-analysis/SKILL.md +56 -0
- package/.agent/skills/brainstorming/SKILL.md +80 -6
- package/.agent/skills/decision-memory/SKILL.md +317 -0
- package/.agent/skills/emergence-detector/SKILL.md +230 -0
- package/.agent/skills/emergence-detector/scripts/check_emergence.py +265 -0
- package/.agent/skills/explained-qa/SKILL.md +142 -0
- package/.agent/skills/explained-qa/game-terminology.md +214 -0
- package/.agent/skills/game-development/ai-dialogue-engine/SKILL.md +442 -0
- package/.agent/skills/game-development/ai-graphics-generator/SKILL.md +463 -0
- package/.agent/skills/game-development/ai-playtest-framework/SKILL.md +570 -0
- package/.agent/skills/game-development/camera-systems/SKILL.md +607 -0
- package/.agent/skills/game-development/card-battle-engine/SKILL.md +618 -0
- package/.agent/skills/game-development/character-controller-3d/SKILL.md +908 -0
- package/.agent/skills/game-development/cloud-save-sync/SKILL.md +527 -0
- package/.agent/skills/game-development/combat-system/SKILL.md +748 -0
- package/.agent/skills/game-development/compliance-rating/SKILL.md +277 -0
- package/.agent/skills/game-development/crossplatform-build/SKILL.md +386 -0
- package/.agent/skills/game-development/cultivation-progression/SKILL.md +520 -0
- package/.agent/skills/game-development/data-driven-balance/SKILL.md +535 -0
- package/.agent/skills/game-development/game-analytics-integrator/SKILL.md +410 -0
- package/.agent/skills/game-development/game-audio-advanced/SKILL.md +646 -0
- package/.agent/skills/game-development/game-economy-designer/SKILL.md +375 -0
- package/.agent/skills/game-development/game-marketing/SKILL.md +85 -0
- package/.agent/skills/game-development/game-state-manager/SKILL.md +883 -0
- package/.agent/skills/game-development/hybrid-game-spec/SKILL.md +220 -0
- package/.agent/skills/game-development/inventory-quest/SKILL.md +747 -0
- package/.agent/skills/game-development/liveops/SKILL.md +308 -0
- package/.agent/skills/game-development/localization/SKILL.md +286 -0
- package/.agent/skills/game-development/mobile-input-patterns/SKILL.md +343 -0
- package/.agent/skills/game-development/monetization-strategy/SKILL.md +94 -0
- package/.agent/skills/game-development/multiplayer-master/SKILL.md +727 -0
- package/.agent/skills/game-development/narrative-branching/SKILL.md +593 -0
- package/.agent/skills/game-development/procedural-level-ai/SKILL.md +367 -0
- package/.agent/skills/game-development/prototyping-rapid/SKILL.md +205 -0
- package/.agent/skills/game-development/spec-ecosystem/SKILL.md +155 -0
- package/.agent/skills/game-development/spec-ecosystem/decision-log-format.md +129 -0
- package/.agent/skills/game-development/spec-ecosystem/templates/PLAN-template.md +178 -0
- package/.agent/skills/game-development/spec-ecosystem/templates/SPEC-template.md +110 -0
- package/.agent/skills/game-development/spec-ecosystem/templates/TASKS-template.md +156 -0
- package/.agent/skills/game-development/survival-systems/SKILL.md +493 -0
- package/.agent/skills/game-development/testing-qa/SKILL.md +270 -0
- package/.agent/skills/game-development/unity-mobile-optimization/SKILL.md +271 -0
- package/.agent/skills/intent-capture/SKILL.md +65 -0
- package/.agent/skills/mcp-composition/SKILL.md +362 -0
- package/.agent/skills/mcp-observability/SKILL.md +323 -0
- package/.agent/skills/mcp-security/SKILL.md +314 -0
- package/.agent/skills/trust-spectrum/SKILL.md +291 -0
- package/.agent/skills/vibe-coding-guard/SKILL.md +328 -0
- package/.agent/templates/AGENTS.game.md +63 -0
- package/.agent/templates/docs/WORKFLOW_GUIDE.en.md +100 -0
- package/.agent/templates/docs/WORKFLOW_GUIDE.vi.md +100 -0
- package/.agent/workflows/ai-agent.md +2 -0
- package/.agent/workflows/autofix.md +1 -0
- package/.agent/workflows/brainstorm.md +1 -0
- package/.agent/workflows/context.md +1 -0
- package/.agent/workflows/create.md +39 -8
- package/.agent/workflows/dashboard.md +1 -0
- package/.agent/workflows/debug.md +14 -0
- package/.agent/workflows/deploy.md +14 -0
- package/.agent/workflows/enhance.md +44 -0
- package/.agent/workflows/gamekit-init.md +177 -0
- package/.agent/workflows/gamekit-launch.md +338 -0
- package/.agent/workflows/gamekit-plan.md +204 -0
- package/.agent/workflows/gamekit-qa.md +153 -0
- package/.agent/workflows/gamekit-spec.md +243 -0
- package/.agent/workflows/gamekit-tasks.md +208 -0
- package/.agent/workflows/marketing.md +2 -0
- package/.agent/workflows/next.md +1 -0
- package/.agent/workflows/orchestrate.md +12 -0
- package/.agent/workflows/pentest.md +2 -0
- package/.agent/workflows/plan.md +42 -0
- package/.agent/workflows/preview.md +1 -0
- package/.agent/workflows/quality.md +1 -0
- package/.agent/workflows/saas.md +2 -0
- package/.agent/workflows/spec.md +42 -0
- package/.agent/workflows/status.md +1 -0
- package/.agent/workflows/test.md +14 -0
- package/.agent/workflows/ui-ux-pro-max.md +1 -0
- package/bin/cli.js +411 -111
- package/package.json +1 -2
- package/.agent/agents/game-asset-curator.md +0 -317
- package/.agent/agents/game-narrative-designer.md +0 -310
- package/.agent/agents/game-qa-agent.md +0 -441
- package/.agent/workflows/game-prototype.md +0 -154
- package/docs/AI_DATA_INFRASTRUCTURE.md +0 -288
- package/docs/CHANGELOG_AI_INFRA.md +0 -141
- package/docs/MIGRATION_GUIDE_V1.9.md +0 -55
|
@@ -0,0 +1,362 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: mcp-composition
|
|
3
|
+
description: Multi-agent orchestration patterns with MCP. Pipelines, hierarchies, and agent-as-server design.
|
|
4
|
+
version: "1.0.0"
|
|
5
|
+
skills:
|
|
6
|
+
- mcp-builder
|
|
7
|
+
- parallel-agents
|
|
8
|
+
- ai-agents-architect
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
# 🎼 MCP Composition Patterns
|
|
12
|
+
|
|
13
|
+
> Build complex multi-agent systems using MCP as the composition layer.
|
|
14
|
+
|
|
15
|
+
---
|
|
16
|
+
|
|
17
|
+
## Quick Reference
|
|
18
|
+
|
|
19
|
+
| Pattern | Use Case |
|
|
20
|
+
|---------|----------|
|
|
21
|
+
| **Pipeline** | Sequential agent processing |
|
|
22
|
+
| **Parallel** | Concurrent multi-agent tasks |
|
|
23
|
+
| **Hierarchical** | Manager-worker orchestration |
|
|
24
|
+
| **Agent-as-Server** | Expose agents as MCP tools |
|
|
25
|
+
|
|
26
|
+
---
|
|
27
|
+
|
|
28
|
+
## 1. Why MCP for Agent Composition?
|
|
29
|
+
|
|
30
|
+
MCP provides a **standardized interface** for agents to:
|
|
31
|
+
- Discover and invoke other agents
|
|
32
|
+
- Share context across agent boundaries
|
|
33
|
+
- Chain operations with type safety
|
|
34
|
+
- Monitor and trace multi-agent flows
|
|
35
|
+
|
|
36
|
+
```
|
|
37
|
+
┌─────────────────────────────────────────────────────────────┐
|
|
38
|
+
│ Multi-Agent System │
|
|
39
|
+
├─────────────────────────────────────────────────────────────┤
|
|
40
|
+
│ │
|
|
41
|
+
│ ┌─────────┐ MCP ┌─────────┐ MCP ┌─────────┐
|
|
42
|
+
│ │ Agent A │◄────────────►│ Agent B │◄────────────►│ Agent C │
|
|
43
|
+
│ │ (Plan) │ │ (Code) │ │ (Review)│
|
|
44
|
+
│ └─────────┘ └─────────┘ └─────────┘
|
|
45
|
+
│ │
|
|
46
|
+
│ Each agent is both an MCP client AND an MCP server │
|
|
47
|
+
│ │
|
|
48
|
+
└─────────────────────────────────────────────────────────────┘
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
---
|
|
52
|
+
|
|
53
|
+
## 2. Pattern: Pipeline (Sequential Chain)
|
|
54
|
+
|
|
55
|
+
Agents process in sequence, each receiving output from the previous.
|
|
56
|
+
|
|
57
|
+
```typescript
|
|
58
|
+
// Pipeline configuration
|
|
59
|
+
const pipeline = [
|
|
60
|
+
{ agent: "planner", tool: "create_plan" },
|
|
61
|
+
{ agent: "coder", tool: "implement_plan" },
|
|
62
|
+
{ agent: "reviewer", tool: "review_code" },
|
|
63
|
+
{ agent: "tester", tool: "write_tests" },
|
|
64
|
+
];
|
|
65
|
+
|
|
66
|
+
// Execute pipeline
|
|
67
|
+
async function executePipeline(initialInput: string) {
|
|
68
|
+
let context = initialInput;
|
|
69
|
+
|
|
70
|
+
for (const stage of pipeline) {
|
|
71
|
+
const result = await mcpClient.callTool(
|
|
72
|
+
stage.agent,
|
|
73
|
+
stage.tool,
|
|
74
|
+
{ input: context }
|
|
75
|
+
);
|
|
76
|
+
|
|
77
|
+
context = result;
|
|
78
|
+
console.log(`[${stage.agent}] completed`);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return context;
|
|
82
|
+
}
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### When to Use
|
|
86
|
+
- ✅ Linear workflows (plan → code → review)
|
|
87
|
+
- ✅ Each stage depends on previous output
|
|
88
|
+
- ✅ Clear handoff points
|
|
89
|
+
|
|
90
|
+
### When to Avoid
|
|
91
|
+
- ❌ Independent parallel tasks
|
|
92
|
+
- ❌ Complex branching logic
|
|
93
|
+
|
|
94
|
+
---
|
|
95
|
+
|
|
96
|
+
## 3. Pattern: Parallel (Fan-Out/Fan-In)
|
|
97
|
+
|
|
98
|
+
Multiple agents work simultaneously on independent tasks.
|
|
99
|
+
|
|
100
|
+
```typescript
|
|
101
|
+
// Parallel execution
|
|
102
|
+
async function parallelAgents(input: string) {
|
|
103
|
+
const tasks = [
|
|
104
|
+
mcpClient.callTool("security-agent", "scan_vulnerabilities", { code: input }),
|
|
105
|
+
mcpClient.callTool("lint-agent", "check_style", { code: input }),
|
|
106
|
+
mcpClient.callTool("test-agent", "find_test_gaps", { code: input }),
|
|
107
|
+
];
|
|
108
|
+
|
|
109
|
+
// Wait for all to complete
|
|
110
|
+
const [security, lint, tests] = await Promise.all(tasks);
|
|
111
|
+
|
|
112
|
+
// Aggregate results
|
|
113
|
+
return {
|
|
114
|
+
security: security.issues,
|
|
115
|
+
lint: lint.warnings,
|
|
116
|
+
tests: tests.gaps,
|
|
117
|
+
overall: computeScore(security, lint, tests),
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
### When to Use
|
|
123
|
+
- ✅ Independent analysis tasks
|
|
124
|
+
- ✅ Multi-perspective review
|
|
125
|
+
- ✅ Time-sensitive operations
|
|
126
|
+
|
|
127
|
+
### When to Avoid
|
|
128
|
+
- ❌ Sequential dependencies
|
|
129
|
+
- ❌ Shared mutable state
|
|
130
|
+
|
|
131
|
+
---
|
|
132
|
+
|
|
133
|
+
## 4. Pattern: Hierarchical (Manager-Worker)
|
|
134
|
+
|
|
135
|
+
A manager agent coordinates specialized worker agents.
|
|
136
|
+
|
|
137
|
+
```typescript
|
|
138
|
+
class OrchestratorAgent {
|
|
139
|
+
private workers = new Map<string, MCPClient>();
|
|
140
|
+
|
|
141
|
+
async handle(request: string) {
|
|
142
|
+
// 1. Analyze request and plan
|
|
143
|
+
const plan = await this.plan(request);
|
|
144
|
+
|
|
145
|
+
// 2. Delegate to workers
|
|
146
|
+
const results = await Promise.all(
|
|
147
|
+
plan.tasks.map(async (task) => {
|
|
148
|
+
const worker = this.selectWorker(task.type);
|
|
149
|
+
return worker.callTool(task.tool, task.args);
|
|
150
|
+
})
|
|
151
|
+
);
|
|
152
|
+
|
|
153
|
+
// 3. Synthesize results
|
|
154
|
+
return this.synthesize(results);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
selectWorker(taskType: string): MCPClient {
|
|
158
|
+
const mapping = {
|
|
159
|
+
"frontend": "frontend-specialist",
|
|
160
|
+
"backend": "backend-specialist",
|
|
161
|
+
"security": "security-auditor",
|
|
162
|
+
"testing": "test-engineer",
|
|
163
|
+
};
|
|
164
|
+
return this.workers.get(mapping[taskType]);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
### When to Use
|
|
170
|
+
- ✅ Complex multi-domain tasks
|
|
171
|
+
- ✅ Dynamic task allocation
|
|
172
|
+
- ✅ Expertise-based routing
|
|
173
|
+
|
|
174
|
+
### When to Avoid
|
|
175
|
+
- ❌ Simple single-domain tasks
|
|
176
|
+
- ❌ When manager overhead > benefit
|
|
177
|
+
|
|
178
|
+
---
|
|
179
|
+
|
|
180
|
+
## 5. Agent-as-MCP-Server Pattern
|
|
181
|
+
|
|
182
|
+
Expose any AI agent as an MCP server for composition.
|
|
183
|
+
|
|
184
|
+
```typescript
|
|
185
|
+
// Wrap agent as MCP server
|
|
186
|
+
function agentToMCPServer(agent: Agent): McpServer {
|
|
187
|
+
const server = new McpServer({
|
|
188
|
+
name: agent.name,
|
|
189
|
+
version: agent.version,
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
// Expose agent capabilities as tools
|
|
193
|
+
for (const capability of agent.capabilities) {
|
|
194
|
+
server.tool(
|
|
195
|
+
capability.name,
|
|
196
|
+
capability.description,
|
|
197
|
+
capability.schema,
|
|
198
|
+
async (args) => {
|
|
199
|
+
const result = await agent.execute(capability.name, args);
|
|
200
|
+
return { content: [{ type: "text", text: JSON.stringify(result) }] };
|
|
201
|
+
}
|
|
202
|
+
);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// Expose agent context as resource
|
|
206
|
+
server.resource(
|
|
207
|
+
"agent-context",
|
|
208
|
+
`${agent.name}://context`,
|
|
209
|
+
async () => ({
|
|
210
|
+
contents: [{
|
|
211
|
+
uri: `${agent.name}://context`,
|
|
212
|
+
mimeType: "application/json",
|
|
213
|
+
text: JSON.stringify(agent.getContext()),
|
|
214
|
+
}]
|
|
215
|
+
})
|
|
216
|
+
);
|
|
217
|
+
|
|
218
|
+
return server;
|
|
219
|
+
}
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
### Benefits
|
|
223
|
+
- 🔄 Any MCP client can invoke the agent
|
|
224
|
+
- 📊 Standard observability
|
|
225
|
+
- 🔐 Unified authentication
|
|
226
|
+
- 🔌 Plugin architecture
|
|
227
|
+
|
|
228
|
+
---
|
|
229
|
+
|
|
230
|
+
## 6. Context Sharing
|
|
231
|
+
|
|
232
|
+
### Shared Memory Pattern
|
|
233
|
+
|
|
234
|
+
```typescript
|
|
235
|
+
// Central context store (e.g., Redis)
|
|
236
|
+
class SharedContext {
|
|
237
|
+
private store: Map<string, any> = new Map();
|
|
238
|
+
|
|
239
|
+
async set(key: string, value: any, ttl?: number) {
|
|
240
|
+
this.store.set(key, { value, expires: Date.now() + (ttl || 3600000) });
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
async get(key: string): Promise<any> {
|
|
244
|
+
const entry = this.store.get(key);
|
|
245
|
+
if (!entry || entry.expires < Date.now()) return null;
|
|
246
|
+
return entry.value;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
// Usage across agents
|
|
251
|
+
const context = new SharedContext();
|
|
252
|
+
|
|
253
|
+
// Agent A writes
|
|
254
|
+
await context.set("current_plan", { steps: [...] });
|
|
255
|
+
|
|
256
|
+
// Agent B reads
|
|
257
|
+
const plan = await context.get("current_plan");
|
|
258
|
+
```
|
|
259
|
+
|
|
260
|
+
### Context Propagation in MCP
|
|
261
|
+
|
|
262
|
+
```typescript
|
|
263
|
+
// Include context in tool calls
|
|
264
|
+
const result = await mcpClient.callTool("coder", "implement", {
|
|
265
|
+
task: "Add login form",
|
|
266
|
+
context: {
|
|
267
|
+
project: "my-app",
|
|
268
|
+
tech_stack: ["React", "TypeScript"],
|
|
269
|
+
decisions: await context.get("decisions"),
|
|
270
|
+
}
|
|
271
|
+
});
|
|
272
|
+
```
|
|
273
|
+
|
|
274
|
+
---
|
|
275
|
+
|
|
276
|
+
## 7. Error Handling & Recovery
|
|
277
|
+
|
|
278
|
+
### Retry Pattern
|
|
279
|
+
|
|
280
|
+
```typescript
|
|
281
|
+
async function callWithRetry(
|
|
282
|
+
agent: string,
|
|
283
|
+
tool: string,
|
|
284
|
+
args: any,
|
|
285
|
+
maxRetries = 3
|
|
286
|
+
) {
|
|
287
|
+
for (let attempt = 1; attempt <= maxRetries; attempt++) {
|
|
288
|
+
try {
|
|
289
|
+
return await mcpClient.callTool(agent, tool, args);
|
|
290
|
+
} catch (error) {
|
|
291
|
+
if (attempt === maxRetries) throw error;
|
|
292
|
+
|
|
293
|
+
// Exponential backoff
|
|
294
|
+
await sleep(Math.pow(2, attempt) * 1000);
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
```
|
|
299
|
+
|
|
300
|
+
### Circuit Breaker
|
|
301
|
+
|
|
302
|
+
```typescript
|
|
303
|
+
class CircuitBreaker {
|
|
304
|
+
private failures = 0;
|
|
305
|
+
private lastFailure = 0;
|
|
306
|
+
private state: "closed" | "open" | "half-open" = "closed";
|
|
307
|
+
|
|
308
|
+
async call<T>(fn: () => Promise<T>): Promise<T> {
|
|
309
|
+
if (this.state === "open") {
|
|
310
|
+
if (Date.now() - this.lastFailure > 30000) {
|
|
311
|
+
this.state = "half-open";
|
|
312
|
+
} else {
|
|
313
|
+
throw new Error("Circuit breaker open");
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
try {
|
|
318
|
+
const result = await fn();
|
|
319
|
+
this.failures = 0;
|
|
320
|
+
this.state = "closed";
|
|
321
|
+
return result;
|
|
322
|
+
} catch (error) {
|
|
323
|
+
this.failures++;
|
|
324
|
+
this.lastFailure = Date.now();
|
|
325
|
+
|
|
326
|
+
if (this.failures >= 5) {
|
|
327
|
+
this.state = "open";
|
|
328
|
+
}
|
|
329
|
+
throw error;
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
```
|
|
334
|
+
|
|
335
|
+
---
|
|
336
|
+
|
|
337
|
+
## 8. Composition Checklist
|
|
338
|
+
|
|
339
|
+
### Before Building
|
|
340
|
+
|
|
341
|
+
- [ ] Define clear agent responsibilities
|
|
342
|
+
- [ ] Choose composition pattern
|
|
343
|
+
- [ ] Design context sharing strategy
|
|
344
|
+
- [ ] Plan error handling
|
|
345
|
+
|
|
346
|
+
### Implementation
|
|
347
|
+
|
|
348
|
+
- [ ] Wrap agents as MCP servers
|
|
349
|
+
- [ ] Implement orchestration logic
|
|
350
|
+
- [ ] Add retry/circuit breaker
|
|
351
|
+
- [ ] Enable distributed tracing
|
|
352
|
+
|
|
353
|
+
### Production
|
|
354
|
+
|
|
355
|
+
- [ ] Monitor inter-agent latency
|
|
356
|
+
- [ ] Set up alerts for failures
|
|
357
|
+
- [ ] Document agent interactions
|
|
358
|
+
- [ ] Version agent APIs
|
|
359
|
+
|
|
360
|
+
---
|
|
361
|
+
|
|
362
|
+
> **Key Insight:** MCP transforms the "how do agents talk to each other?" problem into a solved infrastructure concern, letting you focus on agent capabilities rather than communication protocols.
|
|
@@ -0,0 +1,323 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: mcp-observability
|
|
3
|
+
description: OpenTelemetry integration for MCP servers. Tracing, metrics, LangSmith/Langfuse support.
|
|
4
|
+
version: "1.0.0"
|
|
5
|
+
skills:
|
|
6
|
+
- mcp-builder
|
|
7
|
+
- observability-patterns
|
|
8
|
+
- opentelemetry-expert
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
# 📊 MCP Observability
|
|
12
|
+
|
|
13
|
+
> Comprehensive observability for MCP servers with OpenTelemetry, custom metrics, and LLM-specific tracing.
|
|
14
|
+
|
|
15
|
+
---
|
|
16
|
+
|
|
17
|
+
## Quick Reference
|
|
18
|
+
|
|
19
|
+
| Topic | Description |
|
|
20
|
+
|-------|-------------|
|
|
21
|
+
| **Tracing** | Distributed traces for MCP tool calls |
|
|
22
|
+
| **Metrics** | Latency, throughput, error rates |
|
|
23
|
+
| **LLM Integration** | LangSmith, Langfuse, custom backends |
|
|
24
|
+
|
|
25
|
+
---
|
|
26
|
+
|
|
27
|
+
## 1. Why Observability for MCP?
|
|
28
|
+
|
|
29
|
+
MCP servers are critical infrastructure for AI agents. Without observability:
|
|
30
|
+
- 🔴 Tool failures are invisible
|
|
31
|
+
- 🔴 Performance bottlenecks go unnoticed
|
|
32
|
+
- 🔴 Agent behavior is a "black box"
|
|
33
|
+
- 🔴 Debugging is guesswork
|
|
34
|
+
|
|
35
|
+
With proper observability:
|
|
36
|
+
- ✅ Every tool call is traced
|
|
37
|
+
- ✅ Latency percentiles are visible
|
|
38
|
+
- ✅ Error patterns are detectable
|
|
39
|
+
- ✅ Agent flows are understandable
|
|
40
|
+
|
|
41
|
+
---
|
|
42
|
+
|
|
43
|
+
## 2. OpenTelemetry Integration
|
|
44
|
+
|
|
45
|
+
### Setup
|
|
46
|
+
|
|
47
|
+
```typescript
|
|
48
|
+
import { initializeTracing, traceToolCall } from "./observability/otel.js";
|
|
49
|
+
|
|
50
|
+
// Initialize at startup
|
|
51
|
+
initializeTracing({
|
|
52
|
+
serviceName: "my-mcp-server",
|
|
53
|
+
serviceVersion: "1.0.0",
|
|
54
|
+
otlpEndpoint: "http://localhost:4318",
|
|
55
|
+
});
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
### Trace Tool Calls
|
|
59
|
+
|
|
60
|
+
```typescript
|
|
61
|
+
server.tool("my_tool", "Description", schema, async (args) => {
|
|
62
|
+
// Automatically creates span with timing
|
|
63
|
+
return traceToolCall("my_tool", args, async () => {
|
|
64
|
+
// Tool implementation
|
|
65
|
+
const result = await doWork(args);
|
|
66
|
+
return { content: [{ type: "text", text: result }] };
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
### Trace Structure
|
|
72
|
+
|
|
73
|
+
```
|
|
74
|
+
[TRACE: user-request-123]
|
|
75
|
+
└─ [SPAN: mcp.request] 1200ms
|
|
76
|
+
├─ [SPAN: mcp.tool.search_knowledge] 450ms
|
|
77
|
+
│ ├─ [SPAN: sync.check] 10ms
|
|
78
|
+
│ └─ [SPAN: search.execute] 430ms
|
|
79
|
+
├─ [SPAN: mcp.tool.analyze_dependencies] 300ms
|
|
80
|
+
└─ [SPAN: mcp.tool.get_impact_zone] 400ms
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
---
|
|
84
|
+
|
|
85
|
+
## 3. Metrics Collection
|
|
86
|
+
|
|
87
|
+
### Key Metrics
|
|
88
|
+
|
|
89
|
+
| Metric | Type | Description |
|
|
90
|
+
|--------|------|-------------|
|
|
91
|
+
| `mcp.tool.calls` | Counter | Total tool invocations |
|
|
92
|
+
| `mcp.tool.duration` | Histogram | Tool execution time |
|
|
93
|
+
| `mcp.tool.errors` | Counter | Failed tool calls |
|
|
94
|
+
| `mcp.request.count` | Counter | Total MCP requests |
|
|
95
|
+
| `mcp.sync.duration` | Histogram | Auto-sync execution time |
|
|
96
|
+
|
|
97
|
+
### Collecting Metrics
|
|
98
|
+
|
|
99
|
+
```typescript
|
|
100
|
+
import { recordToolMetrics, getMetrics } from "./observability/otel.js";
|
|
101
|
+
|
|
102
|
+
// Record after each tool call
|
|
103
|
+
recordToolMetrics({
|
|
104
|
+
toolName: "search_knowledge",
|
|
105
|
+
duration: 450,
|
|
106
|
+
success: true,
|
|
107
|
+
inputTokens: 100,
|
|
108
|
+
outputTokens: 500,
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
// Expose metrics endpoint
|
|
112
|
+
server.tool("get_metrics", "Get server metrics", {}, async () => {
|
|
113
|
+
const metrics = getMetrics();
|
|
114
|
+
return { content: [{ type: "text", text: JSON.stringify(metrics) }] };
|
|
115
|
+
});
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
### Metrics Response Example
|
|
119
|
+
|
|
120
|
+
```json
|
|
121
|
+
{
|
|
122
|
+
"tools": {
|
|
123
|
+
"search_knowledge": {
|
|
124
|
+
"calls": 150,
|
|
125
|
+
"avgDuration": 320,
|
|
126
|
+
"p95Duration": 890,
|
|
127
|
+
"errorRate": 0.02
|
|
128
|
+
},
|
|
129
|
+
"analyze_dependencies": {
|
|
130
|
+
"calls": 45,
|
|
131
|
+
"avgDuration": 180,
|
|
132
|
+
"p95Duration": 450,
|
|
133
|
+
"errorRate": 0.0
|
|
134
|
+
}
|
|
135
|
+
},
|
|
136
|
+
"summary": {
|
|
137
|
+
"totalRequests": 195,
|
|
138
|
+
"avgDuration": 280,
|
|
139
|
+
"uptime": 86400
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
---
|
|
145
|
+
|
|
146
|
+
## 4. LangSmith Integration
|
|
147
|
+
|
|
148
|
+
For AI-specific observability with LangChain's LangSmith:
|
|
149
|
+
|
|
150
|
+
```typescript
|
|
151
|
+
// Enable LangSmith tracing
|
|
152
|
+
process.env.LANGCHAIN_TRACING_V2 = "true";
|
|
153
|
+
process.env.LANGSMITH_API_KEY = "your-api-key";
|
|
154
|
+
process.env.LANGSMITH_PROJECT = "mcp-gateway";
|
|
155
|
+
|
|
156
|
+
// Traces automatically sent to LangSmith
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
### Benefits
|
|
160
|
+
- View agent reasoning chains
|
|
161
|
+
- See tool call sequences
|
|
162
|
+
- Track LLM token usage
|
|
163
|
+
- Compare prompt versions
|
|
164
|
+
|
|
165
|
+
---
|
|
166
|
+
|
|
167
|
+
## 5. Langfuse Integration
|
|
168
|
+
|
|
169
|
+
For cost tracking and detailed LLM analytics:
|
|
170
|
+
|
|
171
|
+
```typescript
|
|
172
|
+
import { Langfuse } from "langfuse";
|
|
173
|
+
|
|
174
|
+
const langfuse = new Langfuse({
|
|
175
|
+
publicKey: "pk-...",
|
|
176
|
+
secretKey: "sk-...",
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
// Create trace for MCP session
|
|
180
|
+
const trace = langfuse.trace({
|
|
181
|
+
name: "mcp-session",
|
|
182
|
+
userId: sessionId,
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
// Track tool call
|
|
186
|
+
const span = trace.span({
|
|
187
|
+
name: "tool.search_knowledge",
|
|
188
|
+
input: args,
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
// Update with result
|
|
192
|
+
span.end({
|
|
193
|
+
output: result,
|
|
194
|
+
statusMessage: "success",
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
// Flush at end
|
|
198
|
+
await langfuse.flush();
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
---
|
|
202
|
+
|
|
203
|
+
## 6. Alerting Patterns
|
|
204
|
+
|
|
205
|
+
### Critical Alerts
|
|
206
|
+
|
|
207
|
+
| Condition | Threshold | Action |
|
|
208
|
+
|-----------|-----------|--------|
|
|
209
|
+
| Error rate > 5% | 5 minutes | PagerDuty |
|
|
210
|
+
| P95 latency > 5s | 10 minutes | Slack |
|
|
211
|
+
| Tool unavailable | Any | Immediate |
|
|
212
|
+
|
|
213
|
+
### Warning Alerts
|
|
214
|
+
|
|
215
|
+
| Condition | Threshold | Action |
|
|
216
|
+
|-----------|-----------|--------|
|
|
217
|
+
| Error rate > 1% | 15 minutes | Slack |
|
|
218
|
+
| P95 latency > 2s | 30 minutes | Log |
|
|
219
|
+
| Memory usage > 80% | 5 minutes | Scale |
|
|
220
|
+
|
|
221
|
+
### Alert Configuration (Prometheus/AlertManager)
|
|
222
|
+
|
|
223
|
+
```yaml
|
|
224
|
+
groups:
|
|
225
|
+
- name: mcp-gateway
|
|
226
|
+
rules:
|
|
227
|
+
- alert: MCPHighErrorRate
|
|
228
|
+
expr: rate(mcp_tool_errors_total[5m]) / rate(mcp_tool_calls_total[5m]) > 0.05
|
|
229
|
+
for: 5m
|
|
230
|
+
labels:
|
|
231
|
+
severity: critical
|
|
232
|
+
annotations:
|
|
233
|
+
summary: "MCP Gateway error rate > 5%"
|
|
234
|
+
|
|
235
|
+
- alert: MCPHighLatency
|
|
236
|
+
expr: histogram_quantile(0.95, mcp_tool_duration_seconds_bucket) > 5
|
|
237
|
+
for: 10m
|
|
238
|
+
labels:
|
|
239
|
+
severity: warning
|
|
240
|
+
annotations:
|
|
241
|
+
summary: "MCP Gateway P95 latency > 5s"
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
---
|
|
245
|
+
|
|
246
|
+
## 7. Debugging with Traces
|
|
247
|
+
|
|
248
|
+
### Finding Slow Tools
|
|
249
|
+
|
|
250
|
+
```sql
|
|
251
|
+
-- Query in Jaeger/Tempo
|
|
252
|
+
SELECT
|
|
253
|
+
resource.service.name,
|
|
254
|
+
span.name,
|
|
255
|
+
percentile(duration_ms, 95) as p95
|
|
256
|
+
FROM traces
|
|
257
|
+
WHERE service.name = 'mcp-gateway'
|
|
258
|
+
GROUP BY span.name
|
|
259
|
+
ORDER BY p95 DESC
|
|
260
|
+
LIMIT 10
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
### Tracing Agent Flows
|
|
264
|
+
|
|
265
|
+
```
|
|
266
|
+
Agent: "Find files using authentication"
|
|
267
|
+
│
|
|
268
|
+
├─► [MCP] search_knowledge("authentication") → 3 results
|
|
269
|
+
│
|
|
270
|
+
├─► [MCP] analyze_dependencies("src/auth/login.ts")
|
|
271
|
+
│ └─► imports: 5, imported_by: 12
|
|
272
|
+
│
|
|
273
|
+
└─► [MCP] get_impact_zone("src/auth/login.ts")
|
|
274
|
+
└─► affected: 12 files
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
---
|
|
278
|
+
|
|
279
|
+
## 8. Best Practices
|
|
280
|
+
|
|
281
|
+
### DO ✅
|
|
282
|
+
|
|
283
|
+
- Trace all tool calls
|
|
284
|
+
- Include input/output size in spans
|
|
285
|
+
- Set meaningful span names
|
|
286
|
+
- Propagate trace context
|
|
287
|
+
- Sample appropriately for high volume
|
|
288
|
+
|
|
289
|
+
### DON'T ❌
|
|
290
|
+
|
|
291
|
+
- Log sensitive data in traces
|
|
292
|
+
- Create spans for trivial operations
|
|
293
|
+
- Forget to end spans (use try/finally)
|
|
294
|
+
- Ignore cardinality explosion
|
|
295
|
+
- Over-sample in production
|
|
296
|
+
|
|
297
|
+
### Span Naming Convention
|
|
298
|
+
|
|
299
|
+
```
|
|
300
|
+
mcp.request # Top-level MCP request
|
|
301
|
+
mcp.tool.{name} # Tool execution
|
|
302
|
+
mcp.sync.{target} # Sync operations
|
|
303
|
+
mcp.search.{type} # Search operations
|
|
304
|
+
mcp.auth.{action} # Authentication
|
|
305
|
+
```
|
|
306
|
+
|
|
307
|
+
---
|
|
308
|
+
|
|
309
|
+
## 9. Production Checklist
|
|
310
|
+
|
|
311
|
+
- [ ] OpenTelemetry SDK initialized
|
|
312
|
+
- [ ] OTLP exporter configured
|
|
313
|
+
- [ ] Sampling rate set (1.0 for dev, 0.1 for prod)
|
|
314
|
+
- [ ] Service name and version set
|
|
315
|
+
- [ ] Trace context propagation enabled
|
|
316
|
+
- [ ] Metrics endpoint exposed
|
|
317
|
+
- [ ] Alerts configured
|
|
318
|
+
- [ ] Dashboard created
|
|
319
|
+
- [ ] Log correlation enabled
|
|
320
|
+
|
|
321
|
+
---
|
|
322
|
+
|
|
323
|
+
> **Remember:** Observability is not optional for production MCP servers. The few milliseconds of tracing overhead are worth hours of debugging time saved.
|