@momeidejiyi/dsh-general-mcp 0.3.1 → 0.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index.js +312 -89
- package/package.json +1 -1
- package/src/catalog.ts +5 -8
- package/src/client.ts +7 -1
- package/src/connect.ts +19 -4
- package/src/index.ts +230 -56
- package/src/naming.ts +19 -0
- package/src/sse.ts +89 -25
package/src/sse.ts
CHANGED
|
@@ -50,7 +50,11 @@ export function resolveMessageUrl(endpoint: string, sseUrl: string): string {
|
|
|
50
50
|
} catch {
|
|
51
51
|
return endpoint
|
|
52
52
|
}
|
|
53
|
-
|
|
53
|
+
// Only rewrite when the SSE endpoint itself is remote: a local server that
|
|
54
|
+
// announces another local port is reachable as-is, and rewriting it sent
|
|
55
|
+
// requests to the wrong service. The rewrite exists for NAT'd servers that
|
|
56
|
+
// announce their own loopback address.
|
|
57
|
+
if (target.origin !== sse.origin && isLocalHost(target.hostname) && !isLocalHost(sse.hostname)) {
|
|
54
58
|
target.protocol = sse.protocol
|
|
55
59
|
target.hostname = sse.hostname
|
|
56
60
|
target.port = sse.port
|
|
@@ -86,9 +90,19 @@ export class SseClient {
|
|
|
86
90
|
/** Open the stream and wait for the endpoint announcement. */
|
|
87
91
|
connect(): Promise<void> {
|
|
88
92
|
if (this.connected !== undefined) return this.connected
|
|
89
|
-
|
|
93
|
+
const attempt = (async () => {
|
|
90
94
|
const headers: Record<string, string> = { accept: 'text/event-stream', ...this.headers }
|
|
91
|
-
|
|
95
|
+
// Bound the wait for response headers only: once the stream is open the
|
|
96
|
+
// timer is cleared, so a long-lived SSE connection is not cut off.
|
|
97
|
+
const headerTimer = setTimeout(() => {
|
|
98
|
+
this.controller.abort(new Error('SSE connect timed out after ' + this.timeoutMs + 'ms (GET ' + this.url + ')'))
|
|
99
|
+
}, this.timeoutMs)
|
|
100
|
+
let response: Response
|
|
101
|
+
try {
|
|
102
|
+
response = await fetch(this.url, { headers, signal: this.controller.signal })
|
|
103
|
+
} finally {
|
|
104
|
+
clearTimeout(headerTimer)
|
|
105
|
+
}
|
|
92
106
|
if (!response.ok) throw new Error('SSE connect failed: HTTP ' + response.status)
|
|
93
107
|
if (response.body === null) throw new Error('SSE connect returned no body')
|
|
94
108
|
this.reader = response.body.getReader()
|
|
@@ -100,6 +114,12 @@ export class SseClient {
|
|
|
100
114
|
await announced
|
|
101
115
|
if (this.messageUrl === undefined) throw new Error('SSE server did not announce an endpoint')
|
|
102
116
|
})()
|
|
117
|
+
// A failed connect must abort the GET stream, or every retry leaks one.
|
|
118
|
+
this.connected = attempt.catch(async (error) => {
|
|
119
|
+
this.connected = undefined
|
|
120
|
+
await this.close().catch(() => {})
|
|
121
|
+
throw error
|
|
122
|
+
})
|
|
103
123
|
return this.connected
|
|
104
124
|
}
|
|
105
125
|
|
|
@@ -175,10 +195,38 @@ export class SseClient {
|
|
|
175
195
|
if (waiter !== undefined) { clearTimeout(waiter.timer); waiter.reject(error) }
|
|
176
196
|
}
|
|
177
197
|
|
|
178
|
-
|
|
198
|
+
/**
|
|
199
|
+
* POST one message. The POST itself needs its own deadline: a server that
|
|
200
|
+
* accepts the request and never answers would otherwise keep the caller (and
|
|
201
|
+
* the reply promise) pending forever, and the reply timer would reject with
|
|
202
|
+
* nobody listening.
|
|
203
|
+
*/
|
|
204
|
+
private async post(payload: unknown, signal?: AbortSignal): Promise<Response> {
|
|
205
|
+
const controller = new AbortController()
|
|
206
|
+
const timer = setTimeout(() => {
|
|
207
|
+
controller.abort(new Error('SSE POST timed out after ' + this.timeoutMs + 'ms (POST ' + this.messageUrl + ')'))
|
|
208
|
+
}, this.timeoutMs)
|
|
209
|
+
const forward = () => controller.abort(this.controller.signal.reason)
|
|
210
|
+
this.controller.signal.addEventListener('abort', forward, { once: true })
|
|
211
|
+
const forwardCaller = () => controller.abort(signal?.reason)
|
|
212
|
+
if (signal !== undefined) signal.addEventListener('abort', forwardCaller, { once: true })
|
|
213
|
+
try {
|
|
214
|
+
return await fetch(this.messageUrl as string, {
|
|
215
|
+
method: 'POST',
|
|
216
|
+
headers: { 'content-type': 'application/json', accept: 'application/json, text/event-stream', ...this.headers },
|
|
217
|
+
body: JSON.stringify(payload),
|
|
218
|
+
signal: controller.signal,
|
|
219
|
+
})
|
|
220
|
+
} finally {
|
|
221
|
+
clearTimeout(timer)
|
|
222
|
+
this.controller.signal.removeEventListener('abort', forward)
|
|
223
|
+
if (signal !== undefined) signal.removeEventListener('abort', forwardCaller)
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
private async request(method: string, params: Record<string, unknown>, signal?: AbortSignal): Promise<any> {
|
|
179
228
|
await this.connect()
|
|
180
229
|
const id = (this.nextId += 1)
|
|
181
|
-
const body = JSON.stringify({ jsonrpc: '2.0', id, method, params })
|
|
182
230
|
const reply = new Promise<any>((resolve, reject) => {
|
|
183
231
|
const timer = setTimeout(() => {
|
|
184
232
|
this.pending.delete(id)
|
|
@@ -186,12 +234,17 @@ export class SseClient {
|
|
|
186
234
|
}, this.timeoutMs)
|
|
187
235
|
this.pending.set(id, { resolve, reject, timer })
|
|
188
236
|
})
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
237
|
+
// The POST may fail first; keep the reply from becoming an unhandled
|
|
238
|
+
// rejection that could take the whole host process down.
|
|
239
|
+
reply.catch(() => {})
|
|
240
|
+
let response: Response
|
|
241
|
+
try {
|
|
242
|
+
response = await this.post({ jsonrpc: '2.0', id, method, params }, signal)
|
|
243
|
+
} catch (error) {
|
|
244
|
+
const pending = this.pending.get(id)
|
|
245
|
+
if (pending !== undefined) { clearTimeout(pending.timer); this.pending.delete(id) }
|
|
246
|
+
throw error
|
|
247
|
+
}
|
|
195
248
|
if (!response.ok) {
|
|
196
249
|
const pending = this.pending.get(id)
|
|
197
250
|
if (pending !== undefined) { clearTimeout(pending.timer); this.pending.delete(id) }
|
|
@@ -212,38 +265,49 @@ export class SseClient {
|
|
|
212
265
|
|
|
213
266
|
private async notify(method: string, params: Record<string, unknown>): Promise<void> {
|
|
214
267
|
await this.connect()
|
|
215
|
-
const response = await
|
|
216
|
-
method: 'POST',
|
|
217
|
-
headers: { 'content-type': 'application/json', accept: 'application/json, text/event-stream', ...this.headers },
|
|
218
|
-
body: JSON.stringify({ jsonrpc: '2.0', method, params }),
|
|
219
|
-
signal: this.controller.signal,
|
|
220
|
-
})
|
|
268
|
+
const response = await this.post({ jsonrpc: '2.0', method, params })
|
|
221
269
|
if (!response.ok) throw new Error('notify ' + method + ' failed: HTTP ' + response.status)
|
|
222
270
|
}
|
|
223
271
|
|
|
224
272
|
async listTools(): Promise<SseTool[]> {
|
|
225
|
-
const
|
|
273
|
+
const collected: SseTool[] = []
|
|
274
|
+
const seenCursors = new Set<string>()
|
|
226
275
|
let cursor: string | undefined
|
|
227
276
|
for (let page = 0; page < 100; page += 1) {
|
|
228
277
|
const result: any = await this.request('tools/list', cursor === undefined ? {} : { cursor })
|
|
229
278
|
if (!Array.isArray(result?.tools)) throw new Error('tools/list returned no tools array')
|
|
230
|
-
|
|
279
|
+
collected.push(...result.tools)
|
|
231
280
|
const next = typeof result.nextCursor === 'string' && result.nextCursor !== '' ? result.nextCursor : undefined
|
|
232
|
-
if (next === undefined
|
|
281
|
+
if (next === undefined) { cursor = undefined; break }
|
|
282
|
+
// Same contract as the streamable client: a repeated cursor is a bug in
|
|
283
|
+
// the server, and a single odd entry must not truncate the listing.
|
|
284
|
+
if (seenCursors.has(next)) throw new Error('tools/list repeated cursor ' + next)
|
|
285
|
+
seenCursors.add(next)
|
|
233
286
|
cursor = next
|
|
234
287
|
}
|
|
235
|
-
|
|
288
|
+
if (cursor !== undefined) throw new Error('tools/list did not terminate within 100 pages')
|
|
289
|
+
const byName = new Map<string, SseTool>()
|
|
290
|
+
for (const tool of collected) {
|
|
291
|
+
if (typeof tool?.name !== 'string' || tool.name === '') continue
|
|
292
|
+
byName.set(tool.name, tool)
|
|
293
|
+
}
|
|
294
|
+
return [...byName.values()]
|
|
236
295
|
}
|
|
237
296
|
|
|
238
|
-
async callTool(name: string, args: Record<string, unknown
|
|
239
|
-
const result: any = await this.request('tools/call', { name, arguments: args })
|
|
297
|
+
async callTool(name: string, args: Record<string, unknown>, signal?: AbortSignal): Promise<SseCallResult> {
|
|
298
|
+
const result: any = await this.request('tools/call', { name, arguments: args }, signal)
|
|
240
299
|
return (result ?? {}) as SseCallResult
|
|
241
300
|
}
|
|
242
301
|
|
|
243
302
|
async close(): Promise<void> {
|
|
303
|
+
if (this.closed === true) return
|
|
244
304
|
this.closed = true
|
|
245
305
|
this.failAll(new Error('SSE client closed'))
|
|
246
306
|
this.controller.abort()
|
|
247
|
-
|
|
307
|
+
const reader = this.reader
|
|
308
|
+
this.reader = undefined
|
|
309
|
+
// Never await cancel(): aborting the fetch can leave a pending read in a
|
|
310
|
+
// state where cancel() does not settle, which would wedge close() forever.
|
|
311
|
+
if (reader !== undefined) reader.cancel().catch(() => {})
|
|
248
312
|
}
|
|
249
|
-
}
|
|
313
|
+
}
|