|
| 1 | +export * as McpRegistration from "./registration" |
| 2 | + |
| 3 | +import { ToolFailure } from "@opencode-ai/llm" |
| 4 | +import { Effect, Layer, Schema } from "effect" |
| 5 | +import { makeLocationNode } from "../effect/app-node" |
| 6 | +import { ToolRegistry } from "../tool/registry" |
| 7 | +import { Tool } from "../tool/tool" |
| 8 | +import { Tools } from "../tool/tools" |
| 9 | +import { McpToolSource } from "./tool-source" |
| 10 | + |
| 11 | +const sanitize = (value: string) => value.replace(/[^a-zA-Z0-9_-]/g, "_") |
| 12 | +const toolName = (serverName: string, name: string) => sanitize(serverName) + "_" + sanitize(name) |
| 13 | + |
| 14 | +const Output = Schema.Struct({ |
| 15 | + content: Schema.Array(Schema.Unknown), |
| 16 | + isError: Schema.optional(Schema.Boolean), |
| 17 | + structuredContent: Schema.optional(Schema.Unknown), |
| 18 | +}) |
| 19 | + |
| 20 | +function toModelOutput(output: Schema.Schema.Type<typeof Output>): ReadonlyArray<Tool.Content> { |
| 21 | + const parts: Tool.Content[] = [] |
| 22 | + for (const item of output.content) { |
| 23 | + if (typeof item !== "object" || item === null) continue |
| 24 | + const part = item as Record<string, unknown> |
| 25 | + if (part.type === "text" && typeof part.text === "string") { |
| 26 | + parts.push({ type: "text", text: part.text }) |
| 27 | + } else if (part.type === "image" && typeof part.data === "string" && typeof part.mimeType === "string") { |
| 28 | + parts.push({ type: "file", data: part.data, mime: part.mimeType }) |
| 29 | + } else if (part.type === "resource" && typeof part.resource === "object" && part.resource !== null) { |
| 30 | + const resource = part.resource as Record<string, unknown> |
| 31 | + if (typeof resource.text === "string") parts.push({ type: "text", text: resource.text }) |
| 32 | + if (typeof resource.blob === "string" && typeof resource.mimeType === "string") { |
| 33 | + parts.push({ type: "file", data: resource.blob, mime: resource.mimeType }) |
| 34 | + } |
| 35 | + } |
| 36 | + } |
| 37 | + if (parts.length === 0 && output.structuredContent !== undefined) { |
| 38 | + parts.push({ type: "text", text: JSON.stringify(output.structuredContent) }) |
| 39 | + } |
| 40 | + return parts |
| 41 | +} |
| 42 | + |
| 43 | +const layer = Layer.effectDiscard( |
| 44 | + Effect.gen(function* () { |
| 45 | + const source = yield* McpToolSource.Service |
| 46 | + const tools = yield* Tools.Service |
| 47 | + const entries = yield* source.tools |
| 48 | + |
| 49 | + if (entries.length === 0) return |
| 50 | + |
| 51 | + const record: Record<string, Tool.AnyTool> = {} |
| 52 | + for (const entry of entries) { |
| 53 | + const name = toolName(entry.serverName, entry.def.name) |
| 54 | + record[name] = Tool.make({ |
| 55 | + description: entry.def.description ?? `MCP tool ${entry.def.name} from ${entry.serverName}`, |
| 56 | + input: Schema.Record(Schema.String, Schema.Unknown), |
| 57 | + inputJsonSchema: { |
| 58 | + ...entry.def.inputSchema, |
| 59 | + type: "object", |
| 60 | + properties: (entry.def.inputSchema.properties ?? {}) as Record<string, unknown>, |
| 61 | + additionalProperties: false, |
| 62 | + }, |
| 63 | + output: Output, |
| 64 | + execute: (input) => |
| 65 | + source.execute(entry.serverName, entry.def.name, input, { timeout: entry.timeout }).pipe( |
| 66 | + Effect.mapError( |
| 67 | + (error) => new ToolFailure({ message: error.message }), |
| 68 | + ), |
| 69 | + Effect.flatMap((result) => { |
| 70 | + if (result.isError) { |
| 71 | + const text = result.content |
| 72 | + .filter((item): item is { type: "text"; text: string } => item.type === "text") |
| 73 | + .map((item) => item.text) |
| 74 | + .filter((text) => text.trim()) |
| 75 | + .join("\n\n") |
| 76 | + return Effect.fail(new ToolFailure({ message: text || "MCP tool returned an error" })) |
| 77 | + } |
| 78 | + return Effect.succeed({ |
| 79 | + content: result.content as Array<unknown>, |
| 80 | + isError: result.isError, |
| 81 | + structuredContent: result.structuredContent, |
| 82 | + }) |
| 83 | + }), |
| 84 | + ), |
| 85 | + toModelOutput: ({ output }) => toModelOutput(output), |
| 86 | + }) |
| 87 | + } |
| 88 | + |
| 89 | + yield* tools.register(record).pipe(Effect.orDie) |
| 90 | + }), |
| 91 | +) |
| 92 | + |
| 93 | +export const node = makeLocationNode({ |
| 94 | + name: "mcp-registration", |
| 95 | + layer, |
| 96 | + deps: [ToolRegistry.node, McpToolSource.node], |
| 97 | +}) |
0 commit comments