GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — feat(mcp): add resource read tools (#33483) · sujee/opencode@3f3f120

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • packages
    • opencode
      • src
        • mcp
          • catalog.ts
          • index.ts
        • permission
          • index.ts
        • session
          • prompt.ts
          • tools.ts
      • test/mcp
        • lifecycle.test.ts
        • oauth-auto-connect.test.ts
    • tui/src/component/prompt
      • autocomplete.tsx
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎packages/opencode/src/mcp/catalog.ts‎

Copy file name to clipboardExpand all lines: packages/opencode/src/mcp/catalog.ts
+5-1Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ export function fetch<T extends { name: string }>(
8686
client: Client,
8787
list: (client: Client) => Promise<T[]>,
8888
label: string,
89+
key?: (item: T) => string,
8990
) {
9091
return Effect.tryPromise({
9192
try: () => list(client),
@@ -100,7 +101,10 @@ export function fetch<T extends { name: string }>(
100101
Effect.map((items) => {
101102
const sanitizedClient = sanitize(clientName)
102103
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+
]),
104108
)
105109
}),
106110
Effect.orElseSucceed(() => undefined),
Collapse file

‎packages/opencode/src/mcp/index.ts‎

Copy file name to clipboardExpand all lines: packages/opencode/src/mcp/index.ts
+15-4Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ export interface Interface {
161161
readonly clients: () => Effect.Effect<Record<string, MCPClient>>
162162
readonly tools: () => Effect.Effect<Record<string, Tool>>
163163
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 }>>
165165
readonly add: (name: string, mcp: ConfigMCPV1.Info) => Effect.Effect<{ status: Record<string, Status> | Status }>
166166
readonly connect: (name: string) => Effect.Effect<void, NotFoundError>
167167
readonly disconnect: (name: string) => Effect.Effect<void, NotFoundError>
@@ -654,17 +654,22 @@ export const layer = Layer.effect(
654654
s: State,
655655
listFn: (c: Client, timeout?: number) => Promise<T[]>,
656656
label: string,
657+
key?: (item: T) => string,
658+
targetClientName?: string,
657659
) {
658660
return Effect.gen(function* () {
659661
const cfg = yield* cfgSvc.get()
660662
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+
),
662666
([clientName, client]) =>
663667
McpCatalog.fetch(
664668
clientName,
665669
client,
666670
(c) => listFn(c, requestTimeout(s, clientName, cfg.mcp?.[clientName], cfg.experimental?.mcp_timeout)),
667671
label,
672+
key,
668673
).pipe(Effect.map((items) => Object.entries(items ?? {}))),
669674
{ concurrency: "unbounded" },
670675
).pipe(Effect.map((results) => Object.fromEntries<T & { client: string }>(results.flat())))
@@ -675,8 +680,14 @@ export const layer = Layer.effect(
675680
return yield* collectFromConnected(yield* InstanceState.get(state), McpCatalog.prompts, "prompts")
676681
})
677682

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+
)
680691
})
681692

682693
const withClient = Effect.fnUntraced(function* <A>(
Collapse file

‎packages/opencode/src/permission/index.ts‎

Copy file name to clipboardExpand all lines: packages/opencode/src/permission/index.ts
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,10 @@ export function merge(...rulesets: PermissionV1.Ruleset[]): PermissionV1.Rule[]
214214

215215
export function disabled(tools: string[], ruleset: PermissionV1.Ruleset): Set<string> {
216216
const edits = ["edit", "write", "apply_patch"]
217+
const reads = ["list_mcp_resources", "read_mcp_resource"]
217218
return new Set(
218219
tools.filter((tool) => {
219-
const permission = edits.includes(tool) ? "edit" : tool
220+
const permission = edits.includes(tool) ? "edit" : reads.includes(tool) ? "read" : tool
220221
const rule = ruleset.findLast((rule) => Wildcard.match(permission, rule.permission))
221222
return rule?.pattern === "*" && rule.action === "deny"
222223
}),
Collapse file

‎packages/opencode/src/session/prompt.ts‎

Copy file name to clipboardExpand all lines: packages/opencode/src/session/prompt.ts
+55-5Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,14 @@ globalThis.AI_SDK_LOG_WARNINGS = false
6666

6767
const decodeMessageInfo = Schema.decodeUnknownExit(SessionV1.Info)
6868
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+
])
6977

7078
const STRUCTURED_OUTPUT_DESCRIPTION = `Use this tool to return your final response in the requested structured format.
7179
@@ -77,6 +85,18 @@ IMPORTANT:
7785

7886
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.`
7987

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+
80100
function isOrphanedInterruptedTool(part: SessionV1.ToolPart) {
81101
// cleanup() marks abandoned tool_use blocks this way after retries/aborts.
82102
// They are not pending work and must not trigger an assistant-prefill request.
@@ -731,26 +751,56 @@ export const layer = Layer.effect(
731751
if (!content) throw new Error(`Resource not found: ${clientName}/${uri}`)
732752
const items = Array.isArray(content.contents) ? content.contents : [content.contents]
733753
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) {
735756
pieces.push({
736757
messageID: info.id,
737758
sessionID: input.sessionID,
738759
type: "text",
739760
synthetic: true,
740761
text: c.text,
741762
})
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+
}
744787
pieces.push({
745788
messageID: info.id,
746789
sessionID: input.sessionID,
747790
type: "text",
748791
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}`,
750801
})
751802
}
752803
}
753-
pieces.push({ ...part, messageID: info.id, sessionID: input.sessionID })
754804
} else {
755805
const error = Cause.squash(exit.cause)
756806
yield* Effect.logError("failed to read MCP resource", { error, clientName, uri })

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.