@momeidejiyi/dsh-general-mcp 0.1.7 → 0.1.9

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/src/sse.ts CHANGED
@@ -27,6 +27,38 @@ interface Pending {
27
27
  timer: any
28
28
  }
29
29
 
30
+ /** Hosts that only make sense on the server itself. */
31
+ function isLocalHost(hostname: string): boolean {
32
+ const host = hostname.toLowerCase()
33
+ return host === 'localhost' || host === '127.0.0.1' || host === '::1' || host === '[::1]' ||
34
+ host === '0.0.0.0' || /^10\./.test(host) || /^192\.168\./.test(host) ||
35
+ /^172\.(1[6-9]|2\d|3[01])\./.test(host)
36
+ }
37
+
38
+ /**
39
+ * Spring AI builds the message endpoint from the server's own request URL, so
40
+ * behind a NAT/port-forward it often announces http://localhost:PORT/... That
41
+ * address is unreachable from the client; keep the path and take the origin
42
+ * from the SSE URL we actually connected to.
43
+ */
44
+ export function resolveMessageUrl(endpoint: string, sseUrl: string): string {
45
+ let target: URL
46
+ let sse: URL
47
+ try {
48
+ sse = new URL(sseUrl)
49
+ target = new URL(endpoint, sseUrl)
50
+ } catch {
51
+ return endpoint
52
+ }
53
+ if (target.origin !== sse.origin && isLocalHost(target.hostname)) {
54
+ target.protocol = sse.protocol
55
+ target.hostname = sse.hostname
56
+ target.port = sse.port
57
+ return target.toString()
58
+ }
59
+ return target.toString()
60
+ }
61
+
30
62
  export class SseClient {
31
63
  private messageUrl: string | undefined
32
64
  private pending = new Map<number, Pending>()
@@ -46,6 +78,11 @@ export class SseClient {
46
78
  return '2024-11-05'
47
79
  }
48
80
 
81
+ /** Where requests are POSTed; useful in diagnostics. */
82
+ get messageEndpoint(): string | undefined {
83
+ return this.messageUrl
84
+ }
85
+
49
86
  /** Open the stream and wait for the endpoint announcement. */
50
87
  connect(): Promise<void> {
51
88
  if (this.connected !== undefined) return this.connected
@@ -56,7 +93,7 @@ export class SseClient {
56
93
  if (response.body === null) throw new Error('SSE connect returned no body')
57
94
  this.reader = response.body.getReader()
58
95
  const announced = new Promise<void>((resolve, reject) => {
59
- const timer = setTimeout(() => reject(new Error('SSE endpoint event timed out')), this.timeoutMs)
96
+ const timer = setTimeout(() => reject(new Error('SSE endpoint event timed out after ' + this.timeoutMs + 'ms (GET ' + this.url + ')')), this.timeoutMs)
60
97
  this.endpointWaiter = { resolve, reject, timer }
61
98
  })
62
99
  void this.pump()
@@ -75,7 +112,9 @@ export class SseClient {
75
112
  while (true) {
76
113
  const chunk = await this.reader?.read()
77
114
  if (chunk === undefined || chunk.done === true) break
78
- buffer += decoder.decode(chunk.value, { stream: true })
115
+ // Normalise CRLF here (not per line) so a \r\n split across two chunks
116
+ // still collapses into one event boundary.
117
+ buffer = (buffer + decoder.decode(chunk.value, { stream: true })).replace(/\r\n/g, '\n')
79
118
  let index = buffer.indexOf('\n\n')
80
119
  while (index >= 0) {
81
120
  this.handleEvent(buffer.slice(0, index))
@@ -97,7 +136,7 @@ export class SseClient {
97
136
  }
98
137
  const payload = data.join('\n')
99
138
  if (event === 'endpoint') {
100
- try { this.messageUrl = new URL(payload, this.url).toString() } catch { this.messageUrl = payload }
139
+ this.messageUrl = resolveMessageUrl(payload, this.url)
101
140
  const waiter = this.endpointWaiter
102
141
  this.endpointWaiter = undefined
103
142
  if (waiter !== undefined) { clearTimeout(waiter.timer); waiter.resolve(undefined) }
@@ -143,13 +182,13 @@ export class SseClient {
143
182
  const reply = new Promise<any>((resolve, reject) => {
144
183
  const timer = setTimeout(() => {
145
184
  this.pending.delete(id)
146
- reject(new Error('MCP ' + method + ' timed out after ' + this.timeoutMs + 'ms'))
185
+ reject(new Error('MCP ' + method + ' timed out after ' + this.timeoutMs + 'ms (POST ' + this.messageUrl + ')'))
147
186
  }, this.timeoutMs)
148
187
  this.pending.set(id, { resolve, reject, timer })
149
188
  })
150
189
  const response = await fetch(this.messageUrl as string, {
151
190
  method: 'POST',
152
- headers: { 'content-type': 'application/json', ...this.headers },
191
+ headers: { 'content-type': 'application/json', accept: 'application/json, text/event-stream', ...this.headers },
153
192
  body,
154
193
  signal: this.controller.signal,
155
194
  })
@@ -175,7 +214,7 @@ export class SseClient {
175
214
  await this.connect()
176
215
  const response = await fetch(this.messageUrl as string, {
177
216
  method: 'POST',
178
- headers: { 'content-type': 'application/json', ...this.headers },
217
+ headers: { 'content-type': 'application/json', accept: 'application/json, text/event-stream', ...this.headers },
179
218
  body: JSON.stringify({ jsonrpc: '2.0', method, params }),
180
219
  signal: this.controller.signal,
181
220
  })