Overview
UniSkill Gateway implements the Model Context Protocol (MCP) via Cloudflare Durable Objects, enabling persistent stateful SSE connections for AI agent integrations (Claude Desktop, Cursor, Cline, OpenClaw).Session Lifecycle
Supported MCP Methods
| Method | Behavior |
|---|---|
initialize | Returns protocolVersion: "2024-11-05", server capabilities |
tools/list | Merged tool list: FALLBACK_TOOLS + KV cache + user private tools |
tools/call | Routes to handleExecuteSkill via internal fetch to /v1/execute |
tools/list Resolution
Tool list is built in three layers using a Map for deduplication (higher priority overrides lower):{username}_{skillId} (max 64 chars, special chars → _).
Key Design Decisions
| Decision | Rationale |
|---|---|
| Identity lock at handshake | storedAuthHeader captured at GET time, persists across all POST messages in the session |
| 15s timeout guard | Promise.race prevents a single slow tool call from hanging the entire SSE stream |
| 5s SSE heartbeat | Empty :\n\n comment keeps the TCP connection alive through edge network proxies |
| Graceful degradation | KV failures fall back to FALLBACK_TOOLS — session never returns an empty list |
| Map deduplication | Normalized key (uniskill_ prefix enforced) ensures no duplicate tools regardless of source |
Global Tool Refresh
When skills are updated, trigger a broadcast to all active MCP sessions:mcp_broadcast:tools_changed in KV. Sessions check this key to invalidate their tool list cache on the next tools/list call.
Private Tool Execution Flow
When a user calls a private tool (e.g.alice_my_custom_skill):
- Gateway strips username prefix → resolves actual
skillId - Constructs internal request to
/v1/executewith user’sAuthorizationheader handleExecuteSkillperforms full auth → billing → execution pipeline- Result formatted via
formatToolResponse()→ pushed to SSE stream asevent: message