mcpGatewaySession.ts ×16

Frontier kind: Code frontier

unlabeled · c_1d5a2f135f6e

16 tests · 9457 LOC · 41 files · introduces 0 tests · 91 LOC · 1 file

Introduces — evidence that enters the hierarchy at this concept

Code
16 ranges91 lines · 1 files
Tests
0 tests

Contains — complete concept membership

All code (extent)
1540 ranges9457 lines · 41 files · Browse complete extent
All tests (intent)
16 testsBrowse complete intent

Neighbourhood graph

The orange circle is the focus. Violet and green circles are every ancestor and descendant, broader and narrower, at any distance; blue squares and pink diamonds are the introduced files and exact introduced tests of every visible concept, not only the focus's. Arrows point from broader to narrower concepts and bridge only concepts omitted from this view. Undirected links show source or test introduction. Concept and file size follows LOC; exact test nodes use test-count units.

Introduced files, introduced tests, and structurally relevant concept specialization

In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the native relationship evidence on this page.

Graph controls are ready.

Interactive rendering requires JavaScript and WebGL. Use the native relationship evidence on this page while the interactive map is unavailable.

Native relationship evidence

Every exact file and test below is linked only from the concept that introduces it.

Introduced tests

Every collected test enters the hierarchy at exactly one concept.

No tests are introduced at this concept. Its intent tests are introduced by other concepts.

Introduced code

Every collected source range enters the hierarchy at exactly one concept.

1 file ranked by introduced lines: 91 introduced LOC across 16 ranges. Expand a file to inspect source; the > gutter marks introduced lines.

src/vs/platform/mcp/node/mcpGatewaySession.ts 91 introduced LOC · 16 ranges

Open complete file

1 > /*--------------------------------------------------------------------------------------------- mcpGatewaySession.ts
2 > * Copyright (c) Microsoft Corporation. All rights reserved.
3 > * Licensed under the MIT License. See License.txt in the project root for license information.
4 > *--------------------------------------------------------------------------------------------*/
5 >
6 > import type * as http from 'http';
7 > import {
8 > IJsonRpcNotification, IJsonRpcRequest,
9 > isJsonRpcNotification, isJsonRpcResponse, JsonRpcError, JsonRpcMessage, JsonRpcProtocol, JsonRpcResponse
10 > } from '../../../base/common/jsonRpcProtocol.js';
11 > import { Disposable } from '../../../base/common/lifecycle.js';
12 > import { hasKey } from '../../../base/common/types.js';
13 > import { ILogger } from '../../log/common/log.js';
14 > import { IMcpGatewaySingleServerInvoker } from '../common/mcpGateway.js';
15 > import { MCP } from '../common/modelContextProtocol.js';
16 >
17 > const MCP_LATEST_PROTOCOL_VERSION = '2025-11-25';
18 > const MCP_SUPPORTED_PROTOCOL_VERSIONS = [
19 > '2025-11-25',
20 > '2025-06-18',
21 > '2025-03-26',
22 > '2024-11-05',
23 > '2024-10-07',
24 > ];
25 > const MCP_INVALID_REQUEST = -32600;
26 > const MCP_METHOD_NOT_FOUND = -32601;
27 > const MCP_INVALID_PARAMS = -32602;
28 >
29 > export class McpGatewaySession extends Disposable {
30 > private readonly _rpc: JsonRpcProtocol;
31 > private readonly _sseClients = new Set<http.ServerResponse>();
32 > private _lastEventId = 0;
33 > private _isInitialized = false;
34 >
35 > constructor(
36 > public readonly id: string,
37 > private readonly _logService: ILogger,
38 > private readonly _onDidDispose: () => void,
39 > private readonly _serverInvoker: IMcpGatewaySingleServerInvoker,
40 > ) {
41 > super();
42 >
43 > this._rpc = this._register(new JsonRpcProtocol(
44 > message => this._handleOutgoingMessage(message),
45 > {
46 > handleRequest: request => this._handleRequest(request),
47 > handleNotification: notification => this._handleNotification(notification),
48 > }
49 > ));
50 >
51 > this._register(this._serverInvoker.onDidChangeTools(() => {
52 if (!this._isInitialized) {
53 return;
56 this._logService.info(`[McpGateway][session ${this.id}] Tools changed, notifying client`);
57 this._rpc.sendNotification({ method: 'notifications/tools/list_changed' });
59 >
60 > this._register(this._serverInvoker.onDidChangeResources(() => {
61 if (!this._isInitialized) {
62 return;
65 this._logService.info(`[McpGateway][session ${this.id}] Resources changed, notifying client`);
66 this._rpc.sendNotification({ method: 'notifications/resources/list_changed' });
68 > }
69 >
70 > public attachSseClient(_req: http.IncomingMessage, res: http.ServerResponse): void {
71 res.writeHead(200, {
72 'Content-Type': 'text/event-stream',
84 });
85 }
87 > public async handleIncoming(message: JsonRpcMessage | JsonRpcMessage[]): Promise<JsonRpcResponse[]> {
88 return this._rpc.handleMessage(message);
89 }
91 > public override dispose(): void {
92 > this._logService.info(`[McpGateway][session ${this.id}] Disposing session (SSE clients: ${this._sseClients.size})`);
93 > for (const client of this._sseClients) {
94 if (!client.destroyed) {
95 client.end();
96 }
97 }
98 > this._sseClients.clear(); mcpGatewaySession.ts
99 > this._onDidDispose();
100 > super.dispose();
101 > }
102 >
103 > private _handleOutgoingMessage(message: JsonRpcMessage): void {
104 if (isJsonRpcResponse(message)) {
105 this._logService.debug(`[McpGateway][session ${this.id}] --> response: ${JSON.stringify(message)}`);
115 this._logService.warn('[McpGatewayService] Ignored unsupported outgoing gateway message');
116 }
118 > private _broadcastSse(message: JsonRpcMessage): void {
119 if (this._sseClients.size === 0) {
120 this._logService.debug(`[McpGateway][session ${this.id}] No SSE clients to broadcast to, dropping message`);
143 }
144 }
146 > private async _handleRequest(request: IJsonRpcRequest): Promise<unknown> {
147 this._logService.debug(`[McpGateway][session ${this.id}] <-- request: ${request.method} (id=${String(request.id)})`);
148
174 }
175 }
177 > private _handleNotification(notification: IJsonRpcNotification): void {
178 this._logService.debug(`[McpGateway][session ${this.id}] <-- notification: ${notification.method}`);
179
185 }
186 }
188 > private _handleInitialize(request: IJsonRpcRequest): MCP.InitializeResult {
189 const params = typeof request.params === 'object' && request.params ? request.params as Record<string, unknown> : undefined;
190 const clientVersion = typeof params?.protocolVersion === 'string' ? params.protocolVersion : undefined;
215 };
216 }
218 > private async _handleCallTool(request: IJsonRpcRequest): Promise<MCP.CallToolResult> {
219 const params = typeof request.params === 'object' && request.params ? request.params as Record<string, unknown> : undefined;
220 if (!params || typeof params.name !== 'string') {
241 }
242 }
244 > private async _handleListTools(): Promise<MCP.ListToolsResult> {
245 const tools = await this._serverInvoker.listTools();
246 this._logService.debug(`[McpGateway][session ${this.id}] Listed ${tools.length} tool(s): [${tools.map(t => t.name).join(', ')}]`);
247 return { tools: tools as MCP.Tool[] };
248 }
250 > private async _handleListResources(): Promise<MCP.ListResourcesResult> {
251 const resources = await this._serverInvoker.listResources();
252 this._logService.debug(`[McpGateway][session ${this.id}] Listed ${resources.length} resource(s)`);
253 return { resources: resources as MCP.Resource[] };
254 }
256 > private async _handleReadResource(request: IJsonRpcRequest): Promise<MCP.ReadResourceResult> {
257 const params = typeof request.params === 'object' && request.params ? request.params as Record<string, unknown> : undefined;
258 if (!params || typeof params.uri !== 'string') {
270 }
271 }
273 > private async _handleListResourceTemplates(): Promise<MCP.ListResourceTemplatesResult> {
274 const resourceTemplates = await this._serverInvoker.listResourceTemplates();
275 this._logService.debug(`[McpGateway][session ${this.id}] Listed ${resourceTemplates.length} resource template(s)`);
276 return { resourceTemplates: resourceTemplates as MCP.ResourceTemplate[] };
277 }
279 >
280 > export function isInitializeMessage(message: JsonRpcMessage | JsonRpcMessage[]): boolean {
281 const first = Array.isArray(message) ? message[0] : message;
282 if (!first || !hasKey(first, { method: true })) {