@momeidejiyi/dsh-general-mcp 0.1.5 → 0.1.7
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 +493 -184
- package/package.json +1 -1
- package/src/catalog.ts +2 -0
- package/src/client.ts +1 -1
- 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
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
|
+
}
|