Fix MCP server: create separate McpServer instance per session

Previously shared one McpServer instance across all sessions/transports,
which caused Protocol._transport to be overwritten when new sessions
connected. This broke message routing for requests on previous transports
after long-running SSE streams.

Now creates a new McpServer instance per session, following the stateful
session pattern from the MCP SDK documentation. Each session gets its own
server+transport pair, preventing transport reference conflicts.

Fixes issue where get_agent_status would fail after wait_for_agent completed.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Mohamed Boudra
2025-11-29 21:39:46 +00:00
co-authored by Claude
parent 6b9fbb83c0
commit 36facf4bfd
4 changed files with 190 additions and 94 deletions
@@ -491,6 +491,9 @@ class ClaudeAgentSession implements AgentSession {
permissionMode: this.currentMode,
agents: this.defaults?.agents,
canUseTool: this.handlePermissionRequest,
stderr: (data: string) => {
console.error("[ClaudeAgentSDK]", data.trim());
},
...this.config.extra?.claude,
};