@momeidejiyi/dsh-general-mcp 0.1.4 → 0.1.6
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 +495 -185
- package/package.json +1 -1
- package/src/catalog.ts +2 -0
- package/src/client.ts +3 -2
- package/src/connect.ts +64 -0
- package/src/index.ts +296 -225
- package/src/sse.ts +210 -0
package/package.json
CHANGED
package/src/catalog.ts
CHANGED
|
@@ -20,6 +20,7 @@ export interface ToolDiagnostic {
|
|
|
20
20
|
export interface CatalogEntry {
|
|
21
21
|
toolName: string
|
|
22
22
|
rawName: string
|
|
23
|
+
server: string
|
|
23
24
|
path: string
|
|
24
25
|
virtualPath: string
|
|
25
26
|
folder: string[]
|
|
@@ -144,6 +145,7 @@ export function buildCatalog(
|
|
|
144
145
|
const description = tool.description ?? tool.name
|
|
145
146
|
return {
|
|
146
147
|
toolName: publicName(tool.name),
|
|
148
|
+
server: serverName,
|
|
147
149
|
rawName: tool.name,
|
|
148
150
|
path,
|
|
149
151
|
virtualPath: 'dsh-mcp://' + serverName + '/' + path,
|
package/src/client.ts
CHANGED
|
@@ -22,6 +22,7 @@ export interface McpCallResult {
|
|
|
22
22
|
isError?: boolean
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
+
const CLIENT_VERSION = '0.1.6'
|
|
25
26
|
const MODERN = '2026-07-28'
|
|
26
27
|
const LEGACY = '2025-06-18'
|
|
27
28
|
const META_VERSION = 'io.modelcontextprotocol/protocolVersion'
|
|
@@ -58,7 +59,7 @@ export class McpClient {
|
|
|
58
59
|
const result = await this.request('initialize', {
|
|
59
60
|
protocolVersion: LEGACY,
|
|
60
61
|
capabilities: {},
|
|
61
|
-
clientInfo: { name: 'dsh-general-mcp', version:
|
|
62
|
+
clientInfo: { name: 'dsh-general-mcp', version: CLIENT_VERSION },
|
|
62
63
|
})
|
|
63
64
|
if (typeof (result as any)?.protocolVersion === 'string') this.protocolVersion = (result as any).protocolVersion
|
|
64
65
|
await this.notify('notifications/initialized', {})
|
|
@@ -139,7 +140,7 @@ export class McpClient {
|
|
|
139
140
|
id,
|
|
140
141
|
method,
|
|
141
142
|
params: era === 'modern'
|
|
142
|
-
? { ...params, _meta: { ...(params as any)?._meta, [META_VERSION]: MODERN, [META_CLIENT]: { name: 'dsh-general-mcp', version:
|
|
143
|
+
? { ...params, _meta: { ...(params as any)?._meta, [META_VERSION]: MODERN, [META_CLIENT]: { name: 'dsh-general-mcp', version: CLIENT_VERSION }, [META_CAPS]: {} } }
|
|
143
144
|
: params,
|
|
144
145
|
}
|
|
145
146
|
let response = await this.send(body, signal, method, era, toolName)
|
package/src/connect.ts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/** Pick a transport: Streamable HTTP first, legacy HTTP+SSE as a fallback. */
|
|
2
|
+
import { McpClient, type McpTool } from './client.ts'
|
|
3
|
+
import { SseClient } from './sse.ts'
|
|
4
|
+
|
|
5
|
+
export interface McpConnection {
|
|
6
|
+
transport: 'streamable' | 'sse'
|
|
7
|
+
protocolVersion: string | undefined
|
|
8
|
+
serverInfo: unknown
|
|
9
|
+
initialize(): Promise<void>
|
|
10
|
+
listTools(): Promise<McpTool[]>
|
|
11
|
+
callTool(name: string, args: Record<string, unknown>, signal?: AbortSignal): Promise<{ content?: unknown[]; structuredContent?: unknown; isError?: boolean }>
|
|
12
|
+
close(): Promise<void>
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function looksLikeSse(url: string): boolean {
|
|
16
|
+
try { return new URL(url).pathname.replace(/\/+$/, '').endsWith('/sse') } catch { return false }
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
async function viaStreamable(url: string, headers: Record<string, string>, timeoutMs: number): Promise<McpConnection> {
|
|
20
|
+
const client = new McpClient(url, headers, timeoutMs)
|
|
21
|
+
const info: any = await client.initialize()
|
|
22
|
+
return {
|
|
23
|
+
transport: 'streamable',
|
|
24
|
+
protocolVersion: info?.protocolVersion,
|
|
25
|
+
serverInfo: info?.serverInfo,
|
|
26
|
+
async initialize() {},
|
|
27
|
+
listTools: () => client.listTools(),
|
|
28
|
+
callTool: (name, args, signal) => client.callTool(name, args, signal),
|
|
29
|
+
close: () => client.close(),
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
async function viaSse(url: string, headers: Record<string, string>, timeoutMs: number): Promise<McpConnection> {
|
|
34
|
+
const client = new SseClient(url, headers, timeoutMs)
|
|
35
|
+
const info: any = await client.initialize()
|
|
36
|
+
return {
|
|
37
|
+
transport: 'sse',
|
|
38
|
+
protocolVersion: info?.protocolVersion,
|
|
39
|
+
serverInfo: info?.serverInfo,
|
|
40
|
+
async initialize() {},
|
|
41
|
+
listTools: async () => (await client.listTools()) as unknown as McpTool[],
|
|
42
|
+
callTool: (name, args) => client.callTool(name, args),
|
|
43
|
+
close: () => client.close(),
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export async function connectMcp(
|
|
48
|
+
url: string,
|
|
49
|
+
headers: Record<string, string>,
|
|
50
|
+
timeoutMs: number,
|
|
51
|
+
): Promise<McpConnection> {
|
|
52
|
+
const order: Array<() => Promise<McpConnection>> = looksLikeSse(url)
|
|
53
|
+
? [() => viaSse(url, headers, timeoutMs), () => viaStreamable(url, headers, timeoutMs)]
|
|
54
|
+
: [() => viaStreamable(url, headers, timeoutMs), () => viaSse(url, headers, timeoutMs)]
|
|
55
|
+
const errors: string[] = []
|
|
56
|
+
for (const attempt of order) {
|
|
57
|
+
try {
|
|
58
|
+
return await attempt()
|
|
59
|
+
} catch (error) {
|
|
60
|
+
errors.push(String((error as Error)?.message ?? error))
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
throw new Error('无法连接网关(两种传输都失败): ' + errors.join(' | '))
|
|
64
|
+
}
|