summaryrefslogtreecommitdiffhomepage
path: root/packages/mcp/src/registry.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/mcp/src/registry.ts')
-rw-r--r--packages/mcp/src/registry.ts104
1 files changed, 52 insertions, 52 deletions
diff --git a/packages/mcp/src/registry.ts b/packages/mcp/src/registry.ts
index 7c31c88..e1c95e9 100644
--- a/packages/mcp/src/registry.ts
+++ b/packages/mcp/src/registry.ts
@@ -9,71 +9,71 @@
*/
import type {
- ToolContract,
- ToolExecuteContext,
- ToolParameterSchema,
- ToolResult,
+ ToolContract,
+ ToolExecuteContext,
+ ToolParameterSchema,
+ ToolResult,
} from "@dispatch/kernel";
import type { McpContentItem, McpToolCaller, McpToolInfo } from "./types.js";
const NAMESPACE_SEP = "__";
export function namespace(serverId: string, toolName: string): string {
- return `${serverId}${NAMESPACE_SEP}${toolName}`;
+ return `${serverId}${NAMESPACE_SEP}${toolName}`;
}
export function adaptTool(
- serverId: string,
- mcpTool: McpToolInfo,
- caller: McpToolCaller,
+ serverId: string,
+ mcpTool: McpToolInfo,
+ caller: McpToolCaller,
): ToolContract {
- const parameters: ToolParameterSchema = {
- type: "object",
- ...(mcpTool.inputSchema.properties !== undefined && {
- properties: mcpTool.inputSchema.properties,
- }),
- ...(mcpTool.inputSchema.required !== undefined && {
- required: mcpTool.inputSchema.required,
- }),
- ...(mcpTool.inputSchema.additionalProperties !== undefined && {
- additionalProperties: mcpTool.inputSchema.additionalProperties,
- }),
- };
+ const parameters: ToolParameterSchema = {
+ type: "object",
+ ...(mcpTool.inputSchema.properties !== undefined && {
+ properties: mcpTool.inputSchema.properties,
+ }),
+ ...(mcpTool.inputSchema.required !== undefined && {
+ required: mcpTool.inputSchema.required,
+ }),
+ ...(mcpTool.inputSchema.additionalProperties !== undefined && {
+ additionalProperties: mcpTool.inputSchema.additionalProperties,
+ }),
+ };
- return {
- name: namespace(serverId, mcpTool.name),
- description: `[${serverId}] ${mcpTool.description}`,
- parameters,
- concurrencySafe: false,
- execute: async (args: unknown, ctx: ToolExecuteContext): Promise<ToolResult> => {
- const result = await caller.callTool(mcpTool.name, args, ctx.signal);
- const toolResult: ToolResult = {
- content: flattenContent(result.content),
- };
- if (result.isError !== undefined) {
- (toolResult as { isError?: boolean }).isError = result.isError;
- }
- return toolResult;
- },
- };
+ return {
+ name: namespace(serverId, mcpTool.name),
+ description: `[${serverId}] ${mcpTool.description}`,
+ parameters,
+ concurrencySafe: false,
+ execute: async (args: unknown, ctx: ToolExecuteContext): Promise<ToolResult> => {
+ const result = await caller.callTool(mcpTool.name, args, ctx.signal);
+ const toolResult: ToolResult = {
+ content: flattenContent(result.content),
+ };
+ if (result.isError !== undefined) {
+ (toolResult as { isError?: boolean }).isError = result.isError;
+ }
+ return toolResult;
+ },
+ };
}
export function flattenContent(content: readonly McpContentItem[]): string {
- if (content.length === 0) return "";
+ if (content.length === 0) return "";
- const parts: string[] = [];
- for (const item of content) {
- if (item.type === "text" && item.text !== undefined) {
- parts.push(item.text);
- } else if (item.type === "image" && item.mimeType !== undefined) {
- parts.push(`[image: ${item.mimeType}]`);
- } else if (item.type === "resource" && item.resource !== undefined) {
- if (item.resource.text !== undefined) {
- parts.push(item.resource.text);
- } else {
- parts.push(`[resource: ${item.resource.uri}]`);
- }
- }
- }
- return parts.join("\n");
+ const parts: string[] = [];
+ for (const item of content) {
+ if (item.type === "text" && item.text !== undefined) {
+ parts.push(item.text);
+ } else if (item.type === "image" && item.mimeType !== undefined) {
+ parts.push(`[image: ${item.mimeType}]`);
+ } else if (item.type === "resource" && item.resource !== undefined) {
+ if (item.resource.text !== undefined) {
+ parts.push(item.resource.text);
+ } else {
+ parts.push(`[resource: ${item.resource.uri}]`);
+ }
+ }
+ }
+ return parts.join("\n");
}