ide на основе vs code · VibeGit
ide на основе vs code Это ide на основе vs code(code oss), в ней будет встроенный умный апи роутинг, свой маркет с плагинами(агент харнесс, поддержка java/kotlin/мобильной разработки, командная разработка проектов, orca, оркестратор агентов
krutaya-ide-na-osnove-vs-code / src / vs / platform / mcp / node / mcpGatewaySession.ts 288 строк · изменён 14 нояб., 04:52
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import type * as http from 'http' ;
import {
IJsonRpcNotification, IJsonRpcRequest,
isJsonRpcNotification, isJsonRpcResponse, JsonRpcError, JsonRpcMessage, JsonRpcProtocol, JsonRpcResponse
} from '../../../base/common/jsonRpcProtocol.js' ;
import { Disposable } from '../../../base/common/lifecycle.js' ;
import { hasKey } from '../../../base/common/types.js' ;
import { ILogger } from '../../log/common/log.js' ;
import { IMcpGatewaySingleServerInvoker } from '../common/mcpGateway.js' ;
import
_rpc JsonRpcProtocol
_sseClients
_lastEventId
_isInitialized
id
_logService ILogger
_serverInvoker IMcpGatewaySingleServerInvoker
_rpc
message message
request request
notification notification
_serverInvoker
_isInitialized
_logService
_rpc method
_serverInvoker
_isInitialized
_logService
_rpc method
_req http IncomingMessage res http ServerResponse
res
res
_sseClients res
_logService
res
_sseClients res
_logService
message JsonRpcMessage JsonRpcMessage JsonRpcResponse
_rpc message
override
_logService
client _sseClients
client destroyed
client
_sseClients
message JsonRpcMessage
message
_logService
message
_logService
message
_logService
message JsonRpcMessage
_sseClients size
_logService
payload message
eventId _lastEventId
_logService
lines payload
data
lines line
client _sseClients
client destroyed client writableEnded
_sseClients client
client data
request IJsonRpcRequest
_logService
request method
request
_isInitialized
_logService
request method
request
request
_logService
notification IJsonRpcNotification
_logService
notification method
_isInitialized
_logService
_rpc method
_rpc method
request IJsonRpcRequest InitializeResult
params request params request params request params Record
clientVersion params protocolVersion params protocolVersion
clientInfo params clientInfo name version
negotiatedVersion clientVersion clientVersion
clientVersion
_logService
clientVersion clientVersion negotiatedVersion
_logService
protocolVersion negotiatedVersion
capabilities
tools
listChanged
resources
listChanged
serverInfo
name
version
request IJsonRpcRequest CallToolResult
params request params request params request params Record
params params name
params arguments params arguments
argumentsValue params arguments params arguments
params arguments Record
_logService
result _serverInvoker params name argumentsValue
_logService
result
error
_logService error
error
ListToolsResult
tools _serverInvoker
_logService
tools tools Tool
ListResourcesResult
resources _serverInvoker
_logService
resources resources Resource
request IJsonRpcRequest ReadResourceResult
params request params request params request params Record
params params uri
_logService
result _serverInvoker params uri
_logService
result
error
_logService error
error
ListResourceTemplatesResult
resourceTemplates _serverInvoker
_logService
resourceTemplates resourceTemplates ResourceTemplate
message JsonRpcMessage JsonRpcMessage
first message message message
first first method
first method
{
MCP
}
from
'../common/modelContextProtocol.js'
;
const
MCP_LATEST_PROTOCOL_VERSION
=
'2025-11-25'
;
const
MCP_SUPPORTED_PROTOCOL_VERSIONS
=
[
'2025-11-25'
,
'2025-06-18'
,
'2025-03-26'
,
'2024-11-05'
,
'2024-10-07'
,
]
;
const
MCP_INVALID_REQUEST
=
-
32600
;
const
MCP_METHOD_NOT_FOUND
=
-
32601
;
const
MCP_INVALID_PARAMS
=
-
32602
;
export
class
McpGatewaySession
extends
Disposable
{
private
readonly
:
;
private
readonly
=
new
Set< http. ServerResponse>
(
)
;
private
=
0
;
private
=
false
;
constructor
(
public
readonly
:
string
,
private
readonly
:
,
private
readonly
_onDidDispose
:
(
)
=>
void
,
private
readonly
:
,
)
{
super
(
)
;
this
.
=
this
.
_register
(
new
JsonRpcProtocol
(
=>
this
.
_handleOutgoingMessage
(
)
,
{
handleRequest
:
=>
this
.
_handleRequest
(
)
,
handleNotification
:
=>
this
.
_handleNotification
(
)
,
}
)
)
;
this
.
_register
(
this
.
.
onDidChangeTools
(
(
)
=>
{
if
(
!
this
.
)
{
return
;
}
this
.
.
info
(
` [McpGateway][session ${ this . id} ] Tools changed, notifying client `
)
;
this
.
.
sendNotification
(
{
:
'notifications/tools/list_changed'
}
)
;
}
)
)
;
this
.
_register
(
this
.
.
onDidChangeResources
(
(
)
=>
{
if
(
!
this
.
)
{
return
;
}
this
.
.
info
(
` [McpGateway][session ${ this . id} ] Resources changed, notifying client `
)
;
this
.
.
sendNotification
(
{
:
'notifications/resources/list_changed'
}
)
;
}
)
)
;
}
public
attachSseClient
(
:
.
,
:
.
)
:
void
{
.
writeHead
(
200
,
{
'Content-Type'
:
'text/event-stream'
,
'Cache-Control'
:
'no-cache, no-transform'
,
'Connection'
:
'keep-alive'
,
}
)
;
.
write
(
': connected\n\n'
)
;
this
.
.
add
(
)
;
this
.
.
info
(
` [McpGateway][session ${ this . id} ] SSE client attached (total: ${ this . _sseClients. size} ) `
)
;
.
on
(
'close'
,
(
)
=>
{
this
.
.
delete
(
)
;
this
.
.
info
(
` [McpGateway][session ${ this . id} ] SSE client detached (total: ${ this . _sseClients. size} ) `
)
;
}
)
;
}
public
async
handleIncoming
(
:
|
[
]
)
:
Promise
<
[
]
>
{
return
this
.
.
handleMessage
(
)
;
}
public
dispose
(
)
:
void
{
this
.
.
info
(
` [McpGateway][session ${ this . id} ] Disposing session (SSE clients: ${ this . _sseClients. size} ) `
)
;
for
(
const
of
this
.
)
{
if
(
!
.
)
{
.
end
(
)
;
}
}
this
.
.
clear
(
)
;
this
.
_onDidDispose
(
)
;
super
.
dispose
(
)
;
}
private
_handleOutgoingMessage
(
:
)
:
void
{
if
(
isJsonRpcResponse
(
)
)
{
this
.
.
debug
(
` [McpGateway][session ${ this . id} ] --> response: ${ JSON . stringify ( message) } `
)
;
return
;
}
if
(
isJsonRpcNotification
(
)
)
{
this
.
.
debug
(
` [McpGateway][session ${ this . id} ] --> notification: ${ ( message as IJsonRpcNotification) . method} `
)
;
this
.
_broadcastSse
(
)
;
return
;
}
this
.
.
warn
(
'[McpGatewayService] Ignored unsupported outgoing gateway message'
)
;
}
private
_broadcastSse
(
:
)
:
void
{
if
(
this
.
.
===
0
)
{
this
.
.
debug
(
` [McpGateway][session ${ this . id} ] No SSE clients to broadcast to, dropping message `
)
;
return
;
}
const
=
JSON
.
stringify
(
)
;
const
=
String
(
++
this
.
)
;
this
.
.
debug
(
` [McpGateway][session ${ this . id} ] Broadcasting SSE event id= ${ eventId} to ${ this . _sseClients. size} `
)
;
const
=
.
split
(
/ \r ? \n / g
)
;
const
=
[
` id: ${ eventId} `
,
'event: message'
,
...
.
map
(
=>
` data: ${ line} `
)
,
''
,
''
]
.
join
(
'\n'
)
;
for
(
const
of
[
...
this
.
]
)
{
if
(
.
||
.
)
{
this
.
.
delete
(
)
;
continue
;
}
.
write
(
)
;
}
}
private
async
_handleRequest
(
:
)
:
Promise
<
unknown
>
{
this
.
.
debug
(
` [McpGateway][session ${ this . id} ] <-- request: ${ request. method} (id= ${ String ( request. id) } ) `
)
;
if
(
.
===
'initialize'
)
{
return
this
.
_handleInitialize
(
)
;
}
if
(
!
this
.
)
{
this
.
.
warn
(
` [McpGateway][session ${ this . id} ] Rejected request ' ${ request. method} ': session not initialized `
)
;
throw
new
JsonRpcError
(
MCP_INVALID_REQUEST
,
'Session is not initialized'
)
;
}
switch
(
.
)
{
case
'ping'
:
return
{
}
;
case
'tools/list'
:
return
this
.
_handleListTools
(
)
;
case
'tools/call'
:
return
this
.
_handleCallTool
(
)
;
case
'resources/list'
:
return
this
.
_handleListResources
(
)
;
case
'resources/read'
:
return
this
.
_handleReadResource
(
)
;
case
'resources/templates/list'
:
return
this
.
_handleListResourceTemplates
(
)
;
default
:
this
.
.
warn
(
` [McpGateway][session ${ this . id} ] Unknown method: ${ request. method} `
)
;
throw
new
JsonRpcError
(
MCP_METHOD_NOT_FOUND
,
` Method not found: ${ request. method} `
)
;
}
}
private
_handleNotification
(
:
)
:
void
{
this
.
.
debug
(
` [McpGateway][session ${ this . id} ] <-- notification: ${ notification. method} `
)
;
if
(
.
===
'notifications/initialized'
)
{
this
.
=
true
;
this
.
.
info
(
` [McpGateway][session ${ this . id} ] Session initialized `
)
;
this
.
.
sendNotification
(
{
:
'notifications/tools/list_changed'
}
)
;
this
.
.
sendNotification
(
{
:
'notifications/resources/list_changed'
}
)
;
}
}
private
_handleInitialize
(
:
)
:
MCP
.
{
const
=
typeof
.
===
'object'
&&
.
?
.
as
<
string
,
unknown
>
:
undefined
;
const
=
typeof
?.
===
'string'
?
.
:
undefined
;
const
=
?.
as
{
?
:
string
;
?
:
string
}
|
undefined
;
const
=
&&
MCP_SUPPORTED_PROTOCOL_VERSIONS
.
includes
(
)
?
:
MCP_LATEST_PROTOCOL_VERSION
;
this
.
.
info
(
` [McpGateway] Initialize: client= ${ clientInfo?. name ?? 'unknown' } / ${ clientInfo?. version ?? '?' } , clientProtocol= ${ clientVersion ?? '(none)' } , negotiated= ${ negotiatedVersion} `
)
;
if
(
&&
!==
)
{
this
.
.
warn
(
` [McpGateway] Client requested unsupported protocol version ' ${ clientVersion} ', falling back to ' ${ negotiatedVersion} ' `
)
;
}
return
{
:
,
:
{
:
{
:
true
,
}
,
:
{
:
true
,
}
,
}
,
:
{
:
'VS Code MCP Gateway'
,
:
'1.0.0'
,
}
}
;
}
private
async
_handleCallTool
(
:
)
:
Promise
<
MCP
.
>
{
const
=
typeof
.
===
'object'
&&
.
?
.
as
<
string
,
unknown
>
:
undefined
;
if
(
!
||
typeof
.
!==
'string'
)
{
throw
new
JsonRpcError
(
MCP_INVALID_PARAMS
,
'Missing tool call params'
)
;
}
if
(
.
&&
typeof
.
!==
'object'
)
{
throw
new
JsonRpcError
(
MCP_INVALID_PARAMS
,
'Invalid tool call arguments'
)
;
}
const
=
(
.
&&
typeof
.
===
'object'
)
?
.
as
<
string
,
unknown
>
:
{
}
;
this
.
.
debug
(
` [McpGateway][session ${ this . id} ] Calling tool ' ${ params. name} ' with args: ${ JSON . stringify ( argumentsValue) } `
)
;
try
{
const
=
await
this
.
.
callTool
(
.
,
)
;
this
.
.
debug
(
` [McpGateway][session ${ this . id} ] Tool ' ${ params. name} ' completed (isError= ${ result. isError ?? false } , content blocks= ${ result. content. length} ) `
)
;
return
;
}
catch
(
)
{
this
.
.
error
(
` [McpGateway][session ${ this . id} ] Tool ' ${ params. name} ' invocation failed `
,
)
;
throw
new
JsonRpcError
(
MCP_INVALID_PARAMS
,
String
(
)
)
;
}
}
private
async
_handleListTools
(
)
:
Promise
<
MCP
.
>
{
const
=
await
this
.
.
listTools
(
)
;
this
.
.
debug
(
` [McpGateway][session ${ this . id} ] Listed ${ tools. length} tool(s): [ ${ tools. map ( t => t. name) . join ( ', ' ) } ] `
)
;
return
{
:
as
MCP
.
[
]
}
;
}
private
async
_handleListResources
(
)
:
Promise
<
MCP
.
>
{
const
=
await
this
.
.
listResources
(
)
;
this
.
.
debug
(
` [McpGateway][session ${ this . id} ] Listed ${ resources. length} resource(s) `
)
;
return
{
:
as
MCP
.
[
]
}
;
}
private
async
_handleReadResource
(
:
)
:
Promise
<
MCP
.
>
{
const
=
typeof
.
===
'object'
&&
.
?
.
as
<
string
,
unknown
>
:
undefined
;
if
(
!
||
typeof
.
!==
'string'
)
{
throw
new
JsonRpcError
(
MCP_INVALID_PARAMS
,
'Missing resource URI'
)
;
}
this
.
.
debug
(
` [McpGateway][session ${ this . id} ] Reading resource ' ${ params. uri} ' `
)
;
try
{
const
=
await
this
.
.
readResource
(
.
)
;
this
.
.
debug
(
` [McpGateway][session ${ this . id} ] Resource read returned ${ result. contents. length} content(s) `
)
;
return
;
}
catch
(
)
{
this
.
.
error
(
` [McpGateway][session ${ this . id} ] Resource read failed for ' ${ params. uri} ' `
,
)
;
throw
new
JsonRpcError
(
MCP_INVALID_PARAMS
,
String
(
)
)
;
}
}
private
async
_handleListResourceTemplates
(
)
:
Promise
<
MCP
.
>
{
const
=
await
this
.
.
listResourceTemplates
(
)
;
this
.
.
debug
(
` [McpGateway][session ${ this . id} ] Listed ${ resourceTemplates. length} resource template(s) `
)
;
return
{
:
as
MCP
.
[
]
}
;
}
}
export
function
isInitializeMessage
(
:
|
[
]
)
:
boolean
{
const
=
Array
.
isArray
(
)
?
[
0
]
:
;
if
(
!
||
!
hasKey
(
,
{
:
true
}
)
)
{
return
false
;
}
return
.
===
'initialize'
;
}