| 1 | // Mirror @deepseek-ai/dsh-mcp-client's tool naming and result projection. |
| 2 | import { createHash } from 'node:crypto' |
| 3 | |
| 4 | const MAX_PUBLIC_NAME_LENGTH = 64 |
| 5 | const INVALID_NAME_CHARS = /[^A-Za-z0-9_-]/g |
| 6 | const HASH_LENGTH = 12 |
| 7 | |
| 8 | export function publicToolName(serverName, rawName) { |
| 9 | const joined = `mcp__${serverName}__${rawName}` |
| 10 | const normalized = joined.replace(INVALID_NAME_CHARS, '_') |
| 11 | if (normalized === joined && normalized.length <= MAX_PUBLIC_NAME_LENGTH) return normalized |
| 12 | const hash = createHash('sha256').update(`${serverName}\0${rawName}`).digest('hex').slice(0, HASH_LENGTH) |
| 13 | return `${normalized.slice(0, MAX_PUBLIC_NAME_LENGTH - HASH_LENGTH - 1)}_${hash}` |
| 14 | } |
| 15 | |
| 16 | function extractText(content, rawName) { |
| 17 | if (!Array.isArray(content)) return `(${rawName} returned no text content)` |
| 18 | const parts = [] |
| 19 | for (const value of content) { |
| 20 | if (typeof value !== 'object' || value === null || Array.isArray(value)) { |
| 21 | parts.push('[unsupported content type: unknown]') |
| 22 | continue |
| 23 | } |
| 24 | switch (value.type) { |
| 25 | case 'text': |
| 26 | if (value.text !== undefined) parts.push(value.text) |
| 27 | break |
| 28 | case 'image': |
| 29 | parts.push(`[image: ${value.mimeType ?? 'unknown'}, content discarded]`) |
| 30 | break |
| 31 | case 'audio': |
| 32 | parts.push(`[audio: ${value.mimeType ?? 'unknown'}, content discarded]`) |
| 33 | break |
| 34 | case 'resource': |
| 35 | case 'resource_link': |
| 36 | parts.push('[resource: content discarded]') |
| 37 | break |
| 38 | default: |
| 39 | parts.push(`[unsupported content type: ${value.type}]`) |
| 40 | } |
| 41 | } |
| 42 | return parts.join('\n') || `(${rawName} returned no text content)` |
| 43 | } |
| 44 | |
| 45 | function createOutput(rawName) { |
| 46 | return { |
| 47 | schema: { |
| 48 | type: 'object', |
| 49 | properties: { |
| 50 | content: { type: 'array', items: {} }, |
| 51 | structuredContent: {}, |
| 52 | }, |
| 53 | required: ['content'], |
| 54 | additionalProperties: false, |
| 55 | }, |
| 56 | render(_args, value) { |
| 57 | return [{ type: 'text', text: extractText(value?.content, rawName) }] |
| 58 | }, |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | function createExecutor(client, rawName, taskRequired) { |
| 63 | return async (args, exec) => { |
| 64 | if (taskRequired) { |
| 65 | throw new Error(`Tool "${rawName}" requires task-based execution, which this plugin does not support`) |
| 66 | } |
| 67 | const argsObject = typeof args === 'object' && args !== null ? args : {} |
| 68 | const result = await client.callTool(rawName, argsObject, { signal: exec?.signal }) |
| 69 | const content = Array.isArray(result?.content) ? result.content : [] |
| 70 | const text = extractText(content, rawName) |
| 71 | if (result?.isError === true) throw new Error(text) |
| 72 | return { |
| 73 | content: content.length ? content : [{ type: 'text', text }], |
| 74 | ...(result?.structuredContent === undefined ? {} : { structuredContent: result.structuredContent }), |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Fetch the server's tool list and swap the registered generation. |
| 81 | * Returns the new disposer map; on any failure the previous generation stays. |
| 82 | */ |
| 83 | export async function syncTools(client, ctx, serverName, previous) { |
| 84 | const tools = await client.listTools() |
| 85 | const definitions = new Map() |
| 86 | for (const tool of tools) { |
| 87 | if (!tool || typeof tool.name !== 'string' || !tool.name) continue |
| 88 | const publicName = publicToolName(serverName, tool.name) |
| 89 | if (definitions.has(publicName)) { |
| 90 | throw new Error(`aitoearn(${serverName}): server listed tool "${tool.name}" more than once`) |
| 91 | } |
| 92 | definitions.set(publicName, { |
| 93 | name: publicName, |
| 94 | description: tool.description ?? '', |
| 95 | parameters: tool.inputSchema ?? { type: 'object', properties: {} }, |
| 96 | output: createOutput(tool.name), |
| 97 | execute: createExecutor(client, tool.name, tool.execution?.taskSupport === 'required'), |
| 98 | }) |
| 99 | } |
| 100 | |
| 101 | for (const dispose of previous.values()) dispose() |
| 102 | const disposers = new Map() |
| 103 | try { |
| 104 | for (const [publicName, definition] of definitions) { |
| 105 | disposers.set(publicName, ctx.tools.register(definition)) |
| 106 | } |
| 107 | } catch (error) { |
| 108 | for (const dispose of disposers.values()) dispose() |
| 109 | ctx.logger?.error?.(`aitoearn(${serverName}): tool registration failed: ${String(error)}`) |
| 110 | return new Map() |
| 111 | } |
| 112 | return disposers |
| 113 | } |
| 114 |