MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / MCPSession

Class MCPSession

src/mcp/session.ts:106–350  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

104 * (direct mode) or per socket connection (daemon mode).
105 */
106export class MCPSession {
107 private clientSupportsRoots = false;
108 /** From the initialize handshake — attributes usage rollups to the agent host. */
109 private clientInfo: ClientInfo | undefined;
110 private rootsAttempted = false;
111 private resolvePromise: Promise<void> | null = null;
112 private explicitProjectPath: string | null;
113
114 constructor(
115 private transport: JsonRpcTransport,
116 private engine: MCPEngine,
117 opts: MCPSessionOptions = {},
118 ) {
119 this.explicitProjectPath = opts.explicitProjectPath ?? null;
120 }
121
122 /**
123 * Start handling messages from the transport. Returns immediately — the
124 * session lives for as long as the transport is open.
125 */
126 start(): void {
127 this.transport.start(this.handleMessage.bind(this));
128 }
129
130 /**
131 * Tear down the session. Does NOT touch the engine (the engine may serve
132 * other sessions) or call `process.exit` (the daemon decides when to exit).
133 */
134 stop(): void {
135 this.transport.stop();
136 }
137
138 /** Underlying transport — exposed for daemon-side close hooks. */
139 getTransport(): JsonRpcTransport {
140 return this.transport;
141 }
142
143 private async handleMessage(message: JsonRpcRequest | JsonRpcNotification): Promise<void> {
144 const isRequest = 'id' in message;
145 switch (message.method) {
146 case 'initialize':
147 if (isRequest) await this.handleInitialize(message as JsonRpcRequest);
148 break;
149 case 'initialized':
150 // Notification that client has finished initialization — no action needed.
151 break;
152 case 'tools/list':
153 if (isRequest) await this.handleToolsList(message as JsonRpcRequest);
154 break;
155 case 'tools/call':
156 if (isRequest) await this.handleToolsCall(message as JsonRpcRequest);
157 break;
158 case 'ping':
159 if (isRequest) this.transport.sendResult((message as JsonRpcRequest).id, {});
160 break;
161 case 'resources/list':
162 // We expose no MCP resources, but some clients (opencode, Codex) probe
163 // for them on connect; reply with an empty list instead of a

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected