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;