GeezerVison is showing github.com — feat(mcp): add resource read tools (#33483) · sujee/opencode@3f3f120
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/mcp/catalog.ts +5-1Lines changed: 5 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -86,6 +86,7 @@ export function fetch<T extends { name: string }>( | 86 | 86 | client: Client, | 87 | 87 | list: (client: Client) => Promise<T[]>, | 88 | 88 | label: string, | | 89 | + key?: (item: T) => string, | 89 | 90 | ) { | 90 | 91 | return Effect.tryPromise({ | 91 | 92 | try: () => list(client), | @@ -100,7 +101,10 @@ export function fetch<T extends { name: string }>( | 100 | 101 | Effect.map((items) => { | 101 | 102 | const sanitizedClient = sanitize(clientName) | 102 | 103 | return Object.fromEntries( | 103 | | - items.map((item) => [sanitizedClient + ":" + sanitize(item.name), { ...item, client: clientName }]), | | 104 | + items.map((item) => [ | | 105 | + key ? clientName + ":" + key(item) : sanitizedClient + ":" + sanitize(item.name), | | 106 | + { ...item, client: clientName }, | | 107 | + ]), | 104 | 108 | ) | 105 | 109 | }), | 106 | 110 | Effect.orElseSucceed(() => undefined), | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/mcp/index.ts +15-4Lines changed: 15 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -161,7 +161,7 @@ export interface Interface { | 161 | 161 | readonly clients: () => Effect.Effect<Record<string, MCPClient>> | 162 | 162 | readonly tools: () => Effect.Effect<Record<string, Tool>> | 163 | 163 | readonly prompts: () => Effect.Effect<Record<string, PromptInfo & { client: string }>> | 164 | | - readonly resources: () => Effect.Effect<Record<string, ResourceInfo & { client: string }>> | | 164 | + readonly resources: (clientName?: string) => Effect.Effect<Record<string, ResourceInfo & { client: string }>> | 165 | 165 | readonly add: (name: string, mcp: ConfigMCPV1.Info) => Effect.Effect<{ status: Record<string, Status> | Status }> | 166 | 166 | readonly connect: (name: string) => Effect.Effect<void, NotFoundError> | 167 | 167 | readonly disconnect: (name: string) => Effect.Effect<void, NotFoundError> | @@ -654,17 +654,22 @@ export const layer = Layer.effect( | 654 | 654 | s: State, | 655 | 655 | listFn: (c: Client, timeout?: number) => Promise<T[]>, | 656 | 656 | label: string, | | 657 | + key?: (item: T) => string, | | 658 | + targetClientName?: string, | 657 | 659 | ) { | 658 | 660 | return Effect.gen(function* () { | 659 | 661 | const cfg = yield* cfgSvc.get() | 660 | 662 | return yield* Effect.forEach( | 661 | | - Object.entries(s.clients).filter(([name]) => s.status[name]?.status === "connected"), | | 663 | + Object.entries(s.clients).filter( | | 664 | + ([name]) => s.status[name]?.status === "connected" && (!targetClientName || name === targetClientName), | | 665 | + ), | 662 | 666 | ([clientName, client]) => | 663 | 667 | McpCatalog.fetch( | 664 | 668 | clientName, | 665 | 669 | client, | 666 | 670 | (c) => listFn(c, requestTimeout(s, clientName, cfg.mcp?.[clientName], cfg.experimental?.mcp_timeout)), | 667 | 671 | label, | | 672 | + key, | 668 | 673 | ).pipe(Effect.map((items) => Object.entries(items ?? {}))), | 669 | 674 | { concurrency: "unbounded" }, | 670 | 675 | ).pipe(Effect.map((results) => Object.fromEntries<T & { client: string }>(results.flat()))) | @@ -675,8 +680,14 @@ export const layer = Layer.effect( | 675 | 680 | return yield* collectFromConnected(yield* InstanceState.get(state), McpCatalog.prompts, "prompts") | 676 | 681 | }) | 677 | 682 |
| 678 | | - const resources = Effect.fn("MCP.resources")(function* () { | 679 | | - return yield* collectFromConnected(yield* InstanceState.get(state), McpCatalog.resources, "resources") | | 683 | + const resources = Effect.fn("MCP.resources")(function* (clientName?: string) { | | 684 | + return yield* collectFromConnected( | | 685 | + yield* InstanceState.get(state), | | 686 | + McpCatalog.resources, | | 687 | + "resources", | | 688 | + (resource) => resource.uri, | | 689 | + clientName, | | 690 | + ) | 680 | 691 | }) | 681 | 692 |
| 682 | 693 | const withClient = Effect.fnUntraced(function* <A>( | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/permission/index.ts +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -214,9 +214,10 @@ export function merge(...rulesets: PermissionV1.Ruleset[]): PermissionV1.Rule[] | 214 | 214 |
| 215 | 215 | export function disabled(tools: string[], ruleset: PermissionV1.Ruleset): Set<string> { | 216 | 216 | const edits = ["edit", "write", "apply_patch"] | | 217 | + const reads = ["list_mcp_resources", "read_mcp_resource"] | 217 | 218 | return new Set( | 218 | 219 | tools.filter((tool) => { | 219 | | - const permission = edits.includes(tool) ? "edit" : tool | | 220 | + const permission = edits.includes(tool) ? "edit" : reads.includes(tool) ? "read" : tool | 220 | 221 | const rule = ruleset.findLast((rule) => Wildcard.match(permission, rule.permission)) | 221 | 222 | return rule?.pattern === "*" && rule.action === "deny" | 222 | 223 | }), | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/session/prompt.ts +55-5Lines changed: 55 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -66,6 +66,14 @@ globalThis.AI_SDK_LOG_WARNINGS = false | 66 | 66 |
| 67 | 67 | const decodeMessageInfo = Schema.decodeUnknownExit(SessionV1.Info) | 68 | 68 | const decodeMessagePart = Schema.decodeUnknownExit(SessionV1.Part) | | 69 | +const MAX_MCP_RESOURCE_BLOB_BYTES = 10 * 1024 * 1024 | | 70 | +const SUPPORTED_MCP_RESOURCE_ATTACHMENT_MIMES = new Set([ | | 71 | + "application/pdf", | | 72 | + "image/gif", | | 73 | + "image/jpeg", | | 74 | + "image/png", | | 75 | + "image/webp", | | 76 | +]) | 69 | 77 |
| 70 | 78 | const STRUCTURED_OUTPUT_DESCRIPTION = `Use this tool to return your final response in the requested structured format. | 71 | 79 |
| @@ -77,6 +85,18 @@ IMPORTANT: | 77 | 85 |
| 78 | 86 | const STRUCTURED_OUTPUT_SYSTEM_PROMPT = `IMPORTANT: The user has requested structured output. You MUST use the StructuredOutput tool to provide your final response. Do NOT respond with plain text - you MUST call the StructuredOutput tool with your answer formatted according to the schema.` | 79 | 87 |
| | 88 | +function mcpResourceBase64Size(value: string) { | | 89 | + const trimmed = value.replace(/\s/g, "") | | 90 | + const padding = trimmed.endsWith("==") ? 2 : trimmed.endsWith("=") ? 1 : 0 | | 91 | + return Math.max(0, Math.floor((trimmed.length * 3) / 4) - padding) | | 92 | +} | | 93 | + | | 94 | +function formatMcpResourceBytes(value: number) { | | 95 | + if (value < 1024) return `${value} B` | | 96 | + if (value < 1024 * 1024) return `${Math.ceil(value / 1024)} KB` | | 97 | + return `${Math.ceil(value / (1024 * 1024))} MB` | | 98 | +} | | 99 | + | 80 | 100 | function isOrphanedInterruptedTool(part: SessionV1.ToolPart) { | 81 | 101 | // cleanup() marks abandoned tool_use blocks this way after retries/aborts. | 82 | 102 | // They are not pending work and must not trigger an assistant-prefill request. | @@ -731,26 +751,56 @@ export const layer = Layer.effect( | 731 | 751 | if (!content) throw new Error(`Resource not found: ${clientName}/${uri}`) | 732 | 752 | const items = Array.isArray(content.contents) ? content.contents : [content.contents] | 733 | 753 | for (const c of items) { | 734 | | - if ("text" in c && c.text) { | | 754 | + if (!c || typeof c !== "object") continue | | 755 | + if ("text" in c && typeof c.text === "string" && c.text) { | 735 | 756 | pieces.push({ | 736 | 757 | messageID: info.id, | 737 | 758 | sessionID: input.sessionID, | 738 | 759 | type: "text", | 739 | 760 | synthetic: true, | 740 | 761 | text: c.text, | 741 | 762 | }) | 742 | | - } else if ("blob" in c && c.blob) { | 743 | | - const mime = "mimeType" in c ? c.mimeType : part.mime | | 763 | + } else if ("blob" in c && typeof c.blob === "string" && c.blob) { | | 764 | + const mime = "mimeType" in c && typeof c.mimeType === "string" ? c.mimeType : part.mime | | 765 | + const filename = "uri" in c && typeof c.uri === "string" ? c.uri : part.filename | | 766 | + const size = mcpResourceBase64Size(c.blob) | | 767 | + if (!SUPPORTED_MCP_RESOURCE_ATTACHMENT_MIMES.has(mime)) { | | 768 | + pieces.push({ | | 769 | + messageID: info.id, | | 770 | + sessionID: input.sessionID, | | 771 | + type: "text", | | 772 | + synthetic: true, | | 773 | + text: `[Binary MCP resource omitted: ${filename ?? uri} (${mime}, ${formatMcpResourceBytes(size)}) is not a supported attachment type]`, | | 774 | + }) | | 775 | + continue | | 776 | + } | | 777 | + if (size > MAX_MCP_RESOURCE_BLOB_BYTES) { | | 778 | + pieces.push({ | | 779 | + messageID: info.id, | | 780 | + sessionID: input.sessionID, | | 781 | + type: "text", | | 782 | + synthetic: true, | | 783 | + text: `[Binary MCP resource omitted: ${filename ?? uri} (${mime}, ${formatMcpResourceBytes(size)}) exceeds ${formatMcpResourceBytes(MAX_MCP_RESOURCE_BLOB_BYTES)}]`, | | 784 | + }) | | 785 | + continue | | 786 | + } | 744 | 787 | pieces.push({ | 745 | 788 | messageID: info.id, | 746 | 789 | sessionID: input.sessionID, | 747 | 790 | type: "text", | 748 | 791 | synthetic: true, | 749 | | - text: `[Binary content: ${mime}]`, | | 792 | + text: `[Binary MCP resource attached: ${filename ?? uri} (${mime})]`, | | 793 | + }) | | 794 | + pieces.push({ | | 795 | + messageID: info.id, | | 796 | + sessionID: input.sessionID, | | 797 | + type: "file", | | 798 | + mime, | | 799 | + filename, | | 800 | + url: `data:${mime};base64,${c.blob}`, | 750 | 801 | }) | 751 | 802 | } | 752 | 803 | } | 753 | | - pieces.push({ ...part, messageID: info.id, sessionID: input.sessionID }) | 754 | 804 | } else { | 755 | 805 | const error = Cause.squash(exit.cause) | 756 | 806 | yield* Effect.logError("failed to read MCP resource", { error, clientName, uri }) | |
|