Compare commits

..

4 Commits

Author SHA1 Message Date
Aiden Cline 48e6118640 feat(app): migrate MCP resources 2026-07-07 00:06:12 -05:00
Aiden Cline 988efb6217 feat(tui): support MCP resources 2026-07-06 23:55:36 -05:00
Aiden Cline e1d1352d8f feat(mcp): expose resource APIs 2026-07-06 23:42:29 -05:00
Aiden Cline ab5bd520e2 feat(core): support MCP resources 2026-07-06 23:34:15 -05:00
54 changed files with 2017 additions and 416 deletions
-1
View File
@@ -101,7 +101,6 @@
"dependencies": {
"@effect/platform-node": "catalog:",
"@opencode-ai/client": "workspace:*",
"@opencode-ai/codemode": "workspace:*",
"@opencode-ai/core": "workspace:*",
"@opencode-ai/plugin": "workspace:*",
"@opencode-ai/schema": "workspace:*",
@@ -0,0 +1,79 @@
import { describe, expect, test } from "bun:test"
import { materializeMcpResources } from "./mcp-resource"
describe("MCP resource prompt parts", () => {
test("materializes text and blob content without retaining the resource source", async () => {
const prompt = await materializeMcpResources(
[
{
type: "file",
path: "docs://readme",
content: "@Readme",
start: 0,
end: 7,
filename: "Readme",
source: {
type: "resource",
clientName: "docs",
uri: "docs://readme",
text: { value: "@Readme", start: 0, end: 7 },
},
},
],
async () => ({
server: "docs",
uri: "docs://readme",
contents: [
{ type: "text", uri: "docs://readme", text: "hello", mimeType: "text/plain" },
{ type: "blob", uri: "docs://logo", blob: "aGVsbG8=", mimeType: "image/png" },
],
}),
)
expect(prompt).toEqual([
{
type: "file",
path: "docs://readme",
content: "@Readme",
start: 0,
end: 7,
mime: "text/plain",
filename: "Readme",
url: "data:text/plain;base64,aGVsbG8=",
},
{
type: "file",
path: "docs://logo",
content: "",
start: 0,
end: 0,
mime: "image/png",
filename: "Readme-2",
url: "data:image/png;base64,aGVsbG8=",
},
])
})
test("fails when a resource is unavailable", async () => {
await expect(
materializeMcpResources(
[
{
type: "file",
path: "docs://missing",
content: "@Missing",
start: 0,
end: 8,
source: {
type: "resource",
clientName: "docs",
uri: "docs://missing",
text: { value: "@Missing", start: 0, end: 8 },
},
},
],
async () => null,
),
).rejects.toThrow("Unable to read MCP resource: docs:docs://missing")
})
})
@@ -0,0 +1,47 @@
import type { McpResourceContent } from "@opencode-ai/sdk/v2/client"
import type { FileAttachmentPart, Prompt } from "@/context/prompt"
type ResourceSource = Extract<NonNullable<FileAttachmentPart["source"]>, { type: "resource" }>
export const hasMcpResources = (prompt: Prompt) =>
prompt.some((part) => part.type === "file" && part.source?.type === "resource")
export async function materializeMcpResources(
prompt: Prompt,
read: (source: ResourceSource) => Promise<McpResourceContent | null>,
) {
return (
await Promise.all(
prompt.map(async (part): Promise<Prompt> => {
if (part.type !== "file" || part.source?.type !== "resource") return [part]
const resource = await read(part.source)
if (!resource) throw new Error(`Unable to read MCP resource: ${part.source.clientName}:${part.source.uri}`)
if (resource.contents.length === 0)
throw new Error(`MCP resource returned no content: ${part.source.clientName}:${part.source.uri}`)
return resource.contents.map((content, index) => {
const mime =
content.mimeType ?? part.mime ?? (content.type === "text" ? "text/plain" : "application/octet-stream")
return {
type: "file",
path: content.uri,
content: index === 0 ? part.content : "",
start: index === 0 ? part.start : 0,
end: index === 0 ? part.end : 0,
mime,
filename: index === 0 ? part.filename : `${part.filename ?? "resource"}-${index + 1}`,
url: `data:${mime};base64,${content.type === "text" ? encodeText(content.text) : content.blob}`,
}
})
}),
)
).flat()
}
function encodeText(value: string) {
const bytes = new TextEncoder().encode(value)
const chunks: string[] = []
for (let index = 0; index < bytes.length; index += 0x8000) {
chunks.push(String.fromCharCode(...bytes.subarray(index, index + 0x8000)))
}
return btoa(chunks.join(""))
}
@@ -20,6 +20,7 @@ import { setCursorPosition } from "./editor-dom"
import { formatServerError } from "@/utils/server-errors"
import { ScopedKey } from "@/utils/server-scope"
import { createPromptSubmissionState } from "./submission-state"
import { hasMcpResources, materializeMcpResources } from "./mcp-resource"
type PendingPrompt = {
abort: AbortController
@@ -54,7 +55,6 @@ const draftImages = (prompt: Prompt) => prompt.filter((part): part is ImageAttac
export async function sendFollowupDraft(input: FollowupSendInput) {
const text = draftText(input.draft.prompt)
const images = draftImages(input.draft.prompt)
const setBusy = () => {
if (!input.optimisticBusy) return
input.serverSync.session.set("session_status", input.draft.sessionID, { type: "busy" })
@@ -71,6 +71,19 @@ export async function sendFollowupDraft(input: FollowupSendInput) {
return true
}
const materialize = () =>
materializeMcpResources(input.draft.prompt, async (source) => {
const response = await input.client.v2.mcp.resource.read(
{
location: { directory: input.draft.sessionDirectory },
server: source.clientName,
uri: source.uri,
},
{ throwOnError: true },
)
return response.data.data
})
const [head, ...tail] = text.split(" ")
const cmd = head?.startsWith("/") ? head.slice(1) : undefined
if (cmd && input.sync.data.command.find((item) => item.name === cmd)) {
@@ -81,6 +94,9 @@ export async function sendFollowupDraft(input: FollowupSendInput) {
return false
}
const prompt = hasMcpResources(input.draft.prompt) ? await materialize() : input.draft.prompt
const images = draftImages(prompt)
await input.client.session.command({
sessionID: input.draft.sessionID,
command: cmd,
@@ -88,13 +104,28 @@ export async function sendFollowupDraft(input: FollowupSendInput) {
agent: input.draft.agent,
model: `${input.draft.model.providerID}/${input.draft.model.modelID}`,
variant: input.draft.variant,
parts: images.map((attachment) => ({
id: Identifier.ascending("part"),
type: "file" as const,
mime: attachment.mime,
url: attachment.dataUrl,
filename: attachment.filename,
})),
parts: [
...images.map((attachment) => ({
id: Identifier.ascending("part"),
type: "file" as const,
mime: attachment.mime,
url: attachment.dataUrl,
filename: attachment.filename,
})),
...prompt.flatMap((part) =>
part.type === "file" && part.url?.startsWith("data:")
? [
{
id: Identifier.ascending("part"),
type: "file" as const,
mime: part.mime ?? "text/plain",
url: part.url ?? part.path,
filename: part.filename,
},
]
: [],
),
],
})
return true
} catch (err) {
@@ -103,9 +134,26 @@ export async function sendFollowupDraft(input: FollowupSendInput) {
}
}
let ready = false
let prompt = input.draft.prompt
if (hasMcpResources(prompt)) {
setBusy()
if (!(await wait())) {
setIdle()
return false
}
try {
prompt = await materialize()
} catch (error) {
setIdle()
throw error
}
ready = true
}
const images = draftImages(prompt)
const messageID = input.messageID ?? Identifier.ascending("message")
const { requestParts, optimisticParts } = buildRequestParts({
prompt: input.draft.prompt,
prompt,
context: input.draft.context,
images,
text,
@@ -144,7 +192,7 @@ export async function sendFollowupDraft(input: FollowupSendInput) {
})
try {
if (!(await wait())) {
if (!ready && !(await wait())) {
batch(() => {
setIdle()
remove()
@@ -457,39 +505,6 @@ export function createPromptSubmit(input: PromptSubmitInput) {
return
}
if (text.startsWith("/")) {
const [cmdName, ...args] = text.split(" ")
const commandName = cmdName.slice(1)
const customCommand = sync().data.command.find((c) => c.name === commandName)
if (customCommand) {
clearInput()
client.session
.command({
sessionID: session.id,
command: commandName,
arguments: args.join(" "),
agent,
model: `${model.providerID}/${model.modelID}`,
variant,
parts: images.map((attachment) => ({
id: Identifier.ascending("part"),
type: "file" as const,
mime: attachment.mime,
url: attachment.dataUrl,
filename: attachment.filename,
})),
})
.catch((err) => {
showToast({
title: language.t("prompt.toast.commandSendFailed.title"),
description: formatServerError(err, language.t, language.t("common.requestFailed")),
})
restoreInput()
})
return
}
}
const commentItems = context.filter((item) => item.type === "file" && !!item.comment?.trim())
const messageID = Identifier.ascending("message")
@@ -134,6 +134,28 @@ describe("applyGlobalEvent", () => {
})
describe("applyDirectoryEvent", () => {
test("refreshes MCP state after MCP events", () => {
const [store, setStore] = createStore(baseState())
const calls: string[] = []
const apply = (type: "mcp.status.changed" | "mcp.resources.changed") =>
applyDirectoryEvent({
event: { type, properties: { server: "docs" } },
store,
setStore,
push() {},
directory: "/tmp",
loadLsp() {},
loadMcp: () => calls.push("status"),
loadMcpResources: () => calls.push("resources"),
})
apply("mcp.resources.changed")
expect(calls).toEqual(["resources"])
calls.length = 0
apply("mcp.status.changed")
expect(calls).toEqual(["status", "resources"])
})
test("initializes text delta accumulation from the current part text", () => {
const part = { ...textPart("part", "session", "message"), text: "existing" }
const [store, setStore] = createStore(baseState({ part: { message: [part] } }))
@@ -113,6 +113,8 @@ export function applyDirectoryEvent(input: {
directory: string
loadLsp: () => void
loadReferences?: () => void
loadMcp?: () => void
loadMcpResources?: () => void
vcsCache?: VcsCache
setSessionTodo?: (sessionID: string, todos: Todo[] | undefined) => void
retainedLimit?: number
@@ -409,5 +411,14 @@ export function applyDirectoryEvent(input: {
input.loadReferences?.()
break
}
case "mcp.status.changed": {
input.loadMcp?.()
input.loadMcpResources?.()
break
}
case "mcp.resources.changed": {
input.loadMcpResources?.()
break
}
}
}
+17 -1
View File
@@ -67,7 +67,17 @@ export const loadMcpQuery = (scope: ServerScope, directory: string, sdk: Opencod
export const loadMcpResourcesQuery = (scope: ServerScope, directory: string, sdk: OpencodeClient) =>
queryOptions<Record<string, McpResource>>({
queryKey: [scope, directory, "mcpResources"] as const,
queryFn: () => sdk.experimental.resource.list().then((r) => r.data ?? {}),
queryFn: () =>
sdk.v2.mcp.resource
.catalog({ location: { directory } }, { throwOnError: true })
.then((response) =>
Object.fromEntries(
response.data.data.resources.map((resource) => [
`${encodeURIComponent(resource.server)}:${resource.uri}`,
{ ...resource, client: resource.server },
]),
),
),
placeholderData: {},
})
@@ -416,6 +426,12 @@ export function createServerSyncContextInner(serverSDK: ServerSDK) {
loadReferences: () => {
void queryClient.fetchQuery(queryOptionsApi.references(key))
},
loadMcp: () => {
void queryClient.refetchQueries(queryOptionsApi.mcp(key))
},
loadMcpResources: () => {
void queryClient.refetchQueries(queryOptionsApi.mcpResources(key))
},
})
})
-1
View File
@@ -33,7 +33,6 @@
"dependencies": {
"@effect/platform-node": "catalog:",
"@opencode-ai/client": "workspace:*",
"@opencode-ai/codemode": "workspace:*",
"@opencode-ai/core": "workspace:*",
"@opencode-ai/plugin": "workspace:*",
"@opencode-ai/schema": "workspace:*",
-66
View File
@@ -1,66 +0,0 @@
export * as CodeModeHost from "./code-mode"
import { NodeHttpClient } from "@effect/platform-node"
import { LayerNode } from "@opencode-ai/core/effect/layer-node"
import { ToolRegistry } from "@opencode-ai/core/tool/registry"
import { OpenAPI, Tool } from "@opencode-ai/codemode"
import { Api } from "@opencode-ai/server/api"
import { ServerAuth } from "@opencode-ai/server/auth"
import { Effect, Layer } from "effect"
import { HttpClient, HttpClientRequest } from "effect/unstable/http"
import { OpenApi } from "effect/unstable/httpapi"
import type { Server } from "node:http"
export function replacements(server: Server, password: string): LayerNode.Replacements {
return [ToolRegistry.codeModeReplacement(makeTools(client(server), password))]
}
export function makeTools(client: Layer.Layer<HttpClient.HttpClient>, password: string): ToolRegistry.CodeModeTools {
return {
opencode: bindTools(
OpenAPI.fromSpec({
spec: { ...OpenApi.fromApi(Api) },
baseUrl: "http://opencode.local",
headers: ServerAuth.headers({ username: "opencode", password }),
}).tools,
client,
),
}
}
function client(server: Server) {
return Layer.effect(
HttpClient.HttpClient,
Effect.gen(function* () {
const client = yield* HttpClient.HttpClient
return HttpClient.mapRequest(client, (request) => {
const address = server.address()
if (!address || typeof address === "string") throw new Error("OpenCode server is not listening")
const local =
address.address === "0.0.0.0" ? "127.0.0.1" : address.address === "::" ? "::1" : address.address
const host = local.includes(":") && !local.startsWith("[") ? `[${local}]` : local
const url = new URL(request.url)
return HttpClientRequest.setUrl(
request,
new URL(`${url.pathname}${url.search}${url.hash}`, `http://${host}:${address.port}`),
)
})
}),
).pipe(Layer.provide(NodeHttpClient.layerNodeHttp))
}
function bindTools(tools: OpenAPI.Tools, client: Layer.Layer<HttpClient.HttpClient>): ToolRegistry.CodeModeTools {
return Object.fromEntries(
Object.entries(tools).map(([name, value]) => [
name,
Tool.isDefinition<HttpClient.HttpClient>(value)
? Tool.make({
description: value.description,
input: value.input,
output: value.output,
run: (input) => value.run(input).pipe(Effect.provide(client)),
})
: bindTools(value, client),
]),
)
}
-2
View File
@@ -13,7 +13,6 @@ import { randomBytes, randomUUID } from "node:crypto"
import path from "node:path"
import { Effect, FileSystem, Logger, Option, Redacted, Schedule, Schema } from "effect"
import { HttpServer } from "effect/unstable/http"
import { CodeModeHost } from "./code-mode"
import { Env } from "./env"
import { ServiceConfig } from "./services/service-config"
import { Updater } from "./services/updater"
@@ -64,7 +63,6 @@ const processEffect = Effect.fnUntraced(function* (options: Options) {
hostname: options.hostname ?? config.hostname ?? "127.0.0.1",
port: Option.fromNullishOr(options.port ?? config.port),
password,
replacements: (server) => CodeModeHost.replacements(server, password),
}).pipe(Effect.provide(Logger.layer([], { mergeWithExisting: false })))
if (options.mode === "service") yield* register(address, password)
const url = HttpServer.formatAddress(address)
-39
View File
@@ -1,39 +0,0 @@
import { expect, test } from "bun:test"
import { CodeMode } from "@opencode-ai/codemode"
import { Effect, Layer } from "effect"
import { HttpClient, HttpClientResponse } from "effect/unstable/http"
import { CodeModeHost } from "../src/code-mode"
test("exposes the authenticated OpenCode API through CodeMode", async () => {
const requests: Array<{ readonly url: string; readonly authorization?: string }> = []
const client = Layer.succeed(
HttpClient.HttpClient,
HttpClient.make((request) => {
requests.push({ url: request.url, authorization: request.headers.authorization })
return Effect.succeed(
HttpClientResponse.fromWeb(
request,
Response.json(
{ healthy: true, version: "test", pid: 1 },
{ headers: { "content-type": "application/json" } },
),
),
)
}),
)
const result = await CodeMode.make({ tools: CodeModeHost.makeTools(client, "secret") })
.execute("return await tools.opencode.v2.health.get({})")
.pipe(Effect.runPromise)
expect(result).toEqual({
ok: true,
value: { healthy: true, version: "test", pid: 1 },
toolCalls: [{ name: "opencode.v2.health.get" }],
})
expect(requests).toEqual([
{
url: "http://opencode.local/api/health",
authorization: `Basic ${Buffer.from("opencode:secret").toString("base64")}`,
},
])
})
+20
View File
@@ -439,8 +439,28 @@ export type Endpoint10_0Input = { readonly location?: Endpoint10_0Request["query
export type Endpoint10_0Output = EffectValue<ReturnType<RawClient["server.mcp"]["mcp.list"]>>
export type ServerMcpListOperation<E = never> = (input?: Endpoint10_0Input) => Effect.Effect<Endpoint10_0Output, E>
type Endpoint10_1Request = Parameters<RawClient["server.mcp"]["mcp.resource.catalog"]>[0]
export type Endpoint10_1Input = { readonly location?: Endpoint10_1Request["query"]["location"] }
export type Endpoint10_1Output = EffectValue<ReturnType<RawClient["server.mcp"]["mcp.resource.catalog"]>>
export type ServerMcpResourceCatalogOperation<E = never> = (
input?: Endpoint10_1Input,
) => Effect.Effect<Endpoint10_1Output, E>
type Endpoint10_2Request = Parameters<RawClient["server.mcp"]["mcp.resource.read"]>[0]
export type Endpoint10_2Input = {
readonly location?: Endpoint10_2Request["query"]["location"]
readonly server: Endpoint10_2Request["payload"]["server"]
readonly uri: Endpoint10_2Request["payload"]["uri"]
}
export type Endpoint10_2Output = EffectValue<ReturnType<RawClient["server.mcp"]["mcp.resource.read"]>>
export type ServerMcpReadResourceOperation<E = never> = (
input: Endpoint10_2Input,
) => Effect.Effect<Endpoint10_2Output, E>
export interface ServerMcpApi<E = never> {
readonly list: ServerMcpListOperation<E>
readonly resourceCatalog: ServerMcpResourceCatalogOperation<E>
readonly readResource: ServerMcpReadResourceOperation<E>
}
type Endpoint11_0Request = Parameters<RawClient["server.credential"]["credential.update"]>[0]
+22 -1
View File
@@ -529,7 +529,28 @@ type Endpoint10_0Input = { readonly location?: Endpoint10_0Request["query"]["loc
const Endpoint10_0 = (raw: RawClient["server.mcp"]) => (input?: Endpoint10_0Input) =>
raw["mcp.list"]({ query: { location: input?.["location"] } }).pipe(Effect.mapError(mapClientError))
const adaptGroup10 = (raw: RawClient["server.mcp"]) => ({ list: Endpoint10_0(raw) })
type Endpoint10_1Request = Parameters<RawClient["server.mcp"]["mcp.resource.catalog"]>[0]
type Endpoint10_1Input = { readonly location?: Endpoint10_1Request["query"]["location"] }
const Endpoint10_1 = (raw: RawClient["server.mcp"]) => (input?: Endpoint10_1Input) =>
raw["mcp.resource.catalog"]({ query: { location: input?.["location"] } }).pipe(Effect.mapError(mapClientError))
type Endpoint10_2Request = Parameters<RawClient["server.mcp"]["mcp.resource.read"]>[0]
type Endpoint10_2Input = {
readonly location?: Endpoint10_2Request["query"]["location"]
readonly server: Endpoint10_2Request["payload"]["server"]
readonly uri: Endpoint10_2Request["payload"]["uri"]
}
const Endpoint10_2 = (raw: RawClient["server.mcp"]) => (input: Endpoint10_2Input) =>
raw["mcp.resource.read"]({
query: { location: input["location"] },
payload: { server: input["server"], uri: input["uri"] },
}).pipe(Effect.mapError(mapClientError))
const adaptGroup10 = (raw: RawClient["server.mcp"]) => ({
list: Endpoint10_0(raw),
resourceCatalog: Endpoint10_1(raw),
readResource: Endpoint10_2(raw),
})
type Endpoint11_0Request = Parameters<RawClient["server.credential"]["credential.update"]>[0]
type Endpoint11_0Input = {
+2
View File
@@ -15,6 +15,7 @@ export type {
ProviderApi,
ReferenceApi,
SessionApi,
ServerMcpApi,
SkillApi,
} from "./api.js"
export { Service } from "./service.js"
@@ -27,6 +28,7 @@ export { FileSystem } from "@opencode-ai/schema/filesystem"
export { Form } from "@opencode-ai/schema/form"
export { Integration } from "@opencode-ai/schema/integration"
export { Location } from "@opencode-ai/schema/location"
export { Mcp } from "@opencode-ai/schema/mcp"
export { Model } from "@opencode-ai/schema/model"
export { Permission } from "@opencode-ai/schema/permission"
export { PermissionSaved } from "@opencode-ai/schema/permission-saved"
+2
View File
@@ -8,6 +8,7 @@ import type {
ProviderApi as EffectProviderApi,
ReferenceApi as EffectReferenceApi,
SessionApi as EffectSessionApi,
ServerMcpApi as EffectServerMcpApi,
SkillApi as EffectSkillApi,
} from "../effect/api/api.js"
import type { Effect, Stream } from "effect"
@@ -37,6 +38,7 @@ export type PluginApi = PromisifyApi<EffectPluginApi<unknown>>
export type ProviderApi = PromisifyApi<EffectProviderApi<unknown>>
export type ReferenceApi = PromisifyApi<EffectReferenceApi<unknown>>
export type SessionApi = PromisifyApi<EffectSessionApi<unknown>>
export type ServerMcpApi = PromisifyApi<EffectServerMcpApi<unknown>>
export type SkillApi = PromisifyApi<EffectSkillApi<unknown>>
export interface CatalogApi {
@@ -87,6 +87,10 @@ import type {
IntegrationAttemptCancelOutput,
ServerMcpListInput,
ServerMcpListOutput,
ServerMcpResourceCatalogInput,
ServerMcpResourceCatalogOutput,
ServerMcpReadResourceInput,
ServerMcpReadResourceOutput,
CredentialUpdateInput,
CredentialUpdateOutput,
CredentialRemoveInput,
@@ -890,6 +894,31 @@ export function make(options: ClientOptions) {
},
requestOptions,
),
resourceCatalog: (input?: ServerMcpResourceCatalogInput, requestOptions?: RequestOptions) =>
request<ServerMcpResourceCatalogOutput>(
{
method: "GET",
path: `/api/mcp/resource`,
query: { location: input?.["location"] },
successStatus: 200,
declaredStatuses: [401, 400],
empty: false,
},
requestOptions,
),
readResource: (input: ServerMcpReadResourceInput, requestOptions?: RequestOptions) =>
request<ServerMcpReadResourceOutput>(
{
method: "POST",
path: `/api/mcp/resource/read`,
query: { location: input["location"] },
body: { server: input["server"], uri: input["uri"] },
successStatus: 200,
declaredStatuses: [404, 401, 400],
empty: false,
},
requestOptions,
),
},
credential: {
update: (input: CredentialUpdateInput, requestOptions?: RequestOptions) =>
@@ -106,6 +106,14 @@ export type ProviderNotFoundError = {
export const isProviderNotFoundError = (value: unknown): value is ProviderNotFoundError =>
typeof value === "object" && value !== null && "_tag" in value && value["_tag"] === "ProviderNotFoundError"
export type McpServerNotFoundError = {
readonly _tag: "McpServerNotFoundError"
readonly server: string
readonly message: string
}
export const isMcpServerNotFoundError = (value: unknown): value is McpServerNotFoundError =>
typeof value === "object" && value !== null && "_tag" in value && value["_tag"] === "McpServerNotFoundError"
export type FormNotFoundError = { readonly _tag: "FormNotFoundError"; readonly id: string; readonly message: string }
export const isFormNotFoundError = (value: unknown): value is FormNotFoundError =>
typeof value === "object" && value !== null && "_tag" in value && value["_tag"] === "FormNotFoundError"
@@ -2478,6 +2486,60 @@ export type ServerMcpListOutput = {
}>
}
export type ServerMcpResourceCatalogInput = {
readonly location?: {
readonly location?: { readonly directory?: string | undefined; readonly workspace?: string | undefined } | undefined
}["location"]
}
export type ServerMcpResourceCatalogOutput = {
readonly location: {
readonly directory: string
readonly workspaceID?: string
readonly project: { readonly id: string; readonly directory: string }
}
readonly data: {
readonly resources: ReadonlyArray<{
readonly server: string
readonly name: string
readonly uri: string
readonly description?: string
readonly mimeType?: string
}>
readonly templates: ReadonlyArray<{
readonly server: string
readonly name: string
readonly uriTemplate: string
readonly description?: string
readonly mimeType?: string
}>
}
}
export type ServerMcpReadResourceInput = {
readonly location?: {
readonly location?: { readonly directory?: string | undefined; readonly workspace?: string | undefined } | undefined
}["location"]
readonly server: { readonly server: string; readonly uri: string }["server"]
readonly uri: { readonly server: string; readonly uri: string }["uri"]
}
export type ServerMcpReadResourceOutput = {
readonly location: {
readonly directory: string
readonly workspaceID?: string
readonly project: { readonly id: string; readonly directory: string }
}
readonly data: {
readonly server: string
readonly uri: string
readonly contents: ReadonlyArray<
| { readonly type: "text"; readonly uri: string; readonly text: string; readonly mimeType?: string }
| { readonly type: "blob"; readonly uri: string; readonly blob: string; readonly mimeType?: string }
>
} | null
}
export type CredentialUpdateInput = {
readonly credentialID: { readonly credentialID: string }["credentialID"]
readonly location?: {
@@ -5508,6 +5570,14 @@ export type EventSubscribeOutput =
readonly location?: { readonly directory: string; readonly workspaceID?: string }
readonly data: { readonly server: string }
}
| {
readonly id: string
readonly created: number
readonly metadata?: { readonly [x: string]: unknown }
readonly type: "mcp.resources.changed"
readonly location?: { readonly directory: string; readonly workspaceID?: string }
readonly data: { readonly server: string }
}
| {
readonly id: string
readonly created: number
+1
View File
@@ -10,6 +10,7 @@ export type {
ProviderApi,
ReferenceApi,
SessionApi,
ServerMcpApi,
SkillApi,
} from "./api.js"
export type { EventSubscribeOutput as OpenCodeEvent } from "./generated/types"
@@ -9,6 +9,7 @@ import { SessionInput as CoreSessionInput } from "@opencode-ai/core/session/inpu
import { SessionMessage as CoreSessionMessage } from "@opencode-ai/core/session/message"
import { Agent } from "@opencode-ai/schema/agent"
import { Location } from "@opencode-ai/schema/location"
import { Mcp } from "@opencode-ai/schema/mcp"
import { Model } from "@opencode-ai/schema/model"
import { Project } from "@opencode-ai/schema/project"
import { Provider } from "@opencode-ai/schema/provider"
@@ -26,6 +27,7 @@ const Client = await import("../src/effect")
test("effect entrypoint exposes canonical Schema contracts", () => {
expect(Client.Agent).toBe(Agent)
expect(Client.Model).toBe(Model)
expect(Client.Mcp).toBe(Mcp)
expect(Client.Session).toBe(Session)
})
+69 -1
View File
@@ -1,5 +1,5 @@
import { expect, test } from "bun:test"
import { isSessionNotFoundError, isUnauthorizedError, OpenCode } from "../src/promise/index"
import { isMcpServerNotFoundError, isSessionNotFoundError, isUnauthorizedError, OpenCode } from "../src/promise/index"
test("exposes every standard HTTP API group", () => {
const client = OpenCode.make({ baseUrl: "http://localhost:3000" })
@@ -48,6 +48,74 @@ test("exposes every standard HTTP API group", () => {
expect(Object.keys(client.pty)).toEqual(["list", "create", "get", "update", "remove"])
expect(Object.keys(client.shell)).toEqual(["list", "create", "get", "timeout", "output", "remove"])
expect(Object.keys(client.project)).toEqual(["list", "current", "directories"])
expect(Object.keys(client["server.mcp"])).toEqual(["list", "resourceCatalog", "readResource"])
})
test("MCP resource methods use the public HTTP contract", async () => {
const requests: Array<{ method: string; url: string; body?: unknown }> = []
const client = OpenCode.make({
baseUrl: "http://localhost:3000",
fetch: async (input, init) => {
const request = input instanceof Request ? input : new Request(input, init)
requests.push({
method: request.method,
url: request.url,
body: request.method === "POST" ? await request.json() : undefined,
})
if (request.method === "POST")
return Response.json({
location: { directory: "/tmp/project", project: { id: "proj_test", directory: "/tmp/project" } },
data: {
server: "docs",
uri: "docs://readme",
contents: [{ type: "text", uri: "docs://readme", text: "hello", mimeType: "text/plain" }],
},
})
return Response.json({
location: { directory: "/tmp/project", project: { id: "proj_test", directory: "/tmp/project" } },
data: {
resources: [{ server: "docs", name: "Readme", uri: "docs://readme" }],
templates: [{ server: "docs", name: "File", uriTemplate: "docs://{path}" }],
},
})
},
})
const location = { directory: "/tmp/project" }
expect((await client["server.mcp"].resourceCatalog({ location })).data.resources[0]?.uri).toBe("docs://readme")
expect(
(await client["server.mcp"].readResource({ server: "docs", uri: "docs://readme", location })).data?.contents,
).toEqual([{ type: "text", uri: "docs://readme", text: "hello", mimeType: "text/plain" }])
expect(requests).toEqual([
{
method: "GET",
url: "http://localhost:3000/api/mcp/resource?location%5Bdirectory%5D=%2Ftmp%2Fproject",
body: undefined,
},
{
method: "POST",
url: "http://localhost:3000/api/mcp/resource/read?location%5Bdirectory%5D=%2Ftmp%2Fproject",
body: { server: "docs", uri: "docs://readme" },
},
])
})
test("MCP resource reads preserve unknown-server errors", async () => {
const client = OpenCode.make({
baseUrl: "http://localhost:3000",
fetch: async () =>
Response.json(
{ _tag: "McpServerNotFoundError", server: "missing", message: "MCP server not found: missing" },
{ status: 404 },
),
})
try {
await client["server.mcp"].readResource({ server: "missing", uri: "docs://readme" })
throw new Error("Expected request to fail")
} catch (error) {
expect(isMcpServerNotFoundError(error)).toBe(true)
}
})
test("file.read returns binary content from the public HTTP contract", async () => {
+234 -17
View File
@@ -21,15 +21,24 @@ import {
ListToolsResultSchema,
PromptListChangedNotificationSchema,
PromptSchema,
ResourceListChangedNotificationSchema,
ResourceUpdatedNotificationSchema,
type LoggingMessageNotification,
LoggingMessageNotificationSchema,
ToolListChangedNotificationSchema,
ToolSchema,
} from "@modelcontextprotocol/sdk/types.js"
import { Cause, Effect, Exit, Schema } from "effect"
import { Cause, Effect, Exit, Schema, Scope, Semaphore } from "effect"
import { ConfigMCP } from "../config/mcp"
import { InstallationVersion } from "../installation/version"
declare module "@modelcontextprotocol/sdk/client/streamableHttp.js" {
interface StreamableHTTPClientTransport {
/** Added by the repository's MCP SDK session-recovery patch. */
onsessionexpired?: () => Promise<void>
}
}
const DEFAULT_STARTUP_TIMEOUT = 30_000
const DEFAULT_CATALOG_TIMEOUT = 30_000
const DEFAULT_EXECUTION_TIMEOUT = 12 * 60 * 60 * 1_000 // 12 hours
@@ -68,11 +77,13 @@ export interface ToolDefinition {
export interface PromptDefinition {
readonly name: string
readonly description: string | undefined
readonly arguments: ReadonlyArray<{
readonly name: string
readonly description: string | undefined
readonly required: boolean | undefined
}> | undefined
readonly arguments:
| ReadonlyArray<{
readonly name: string
readonly description: string | undefined
readonly required: boolean | undefined
}>
| undefined
}
export interface PromptMessage {
@@ -84,6 +95,28 @@ export interface PromptResult {
readonly messages: ReadonlyArray<PromptMessage>
}
export interface ResourceDefinition {
readonly name: string
readonly uri: string
readonly description: string | undefined
readonly mimeType: string | undefined
}
export interface ResourceTemplateDefinition {
readonly name: string
readonly uriTemplate: string
readonly description: string | undefined
readonly mimeType: string | undefined
}
export type ResourceContentPart =
| { readonly type: "text"; readonly uri: string; readonly text: string; readonly mimeType: string | undefined }
| { readonly type: "blob"; readonly uri: string; readonly blob: string; readonly mimeType: string | undefined }
export interface ReadResourceResult {
readonly contents: ReadonlyArray<ResourceContentPart>
}
export type CallToolContent =
| { readonly type: "text"; readonly text: string }
| { readonly type: "media"; readonly data: string; readonly mimeType: string }
@@ -124,6 +157,17 @@ export interface Connection {
readonly tools: () => Effect.Effect<ToolDefinition[], Error>
/** Lists the server's prompts; returns [] when the server doesn't advertise prompt support, fails on a transport error. */
readonly prompts: () => Effect.Effect<PromptDefinition[], Error>
/** Lists the server's resources; returns [] when the server doesn't advertise resource support. */
readonly resources: () => Effect.Effect<ResourceDefinition[], Error>
/** Lists the server's resource templates; returns [] when the server doesn't advertise resource support. */
readonly resourceTemplates: () => Effect.Effect<ResourceTemplateDefinition[], Error>
/** Reads one resource; returns undefined when the server doesn't advertise resource support. */
readonly readResource: (input: { readonly uri: string }) => Effect.Effect<ReadResourceResult | undefined, Error>
/** Subscribes for the lifetime of the current scope; duplicate local listeners share one server subscription. */
readonly subscribeResource: (
input: { readonly uri: string },
callback: () => void,
) => Effect.Effect<boolean, Error, Scope.Scope>
/** Invokes a prompt on the server. Interruption aborts the in-flight request. */
readonly prompt: (input: {
readonly name: string
@@ -141,6 +185,8 @@ export interface Connection {
readonly onToolsChanged: (callback: () => void) => void
/** Registers a callback fired when the server announces its prompt list changed; no-op if unsupported. */
readonly onPromptsChanged: (callback: () => void) => void
/** Registers a callback fired when the server announces its resource catalog changed or its HTTP session recovers. */
readonly onResourcesChanged: (callback: () => void) => void
}
/** Connects an MCP server; closing the calling scope tears down the transport and any spawned process. */
@@ -168,7 +214,8 @@ export const connect = Effect.fnUntraced(function* (
},
})
}
if (!URL.canParse(config.url)) return yield* new ConnectError({ server, message: `Invalid MCP URL for "${server}"` })
if (!URL.canParse(config.url))
return yield* new ConnectError({ server, message: `Invalid MCP URL for "${server}"` })
return new StreamableHTTPClientTransport(new URL(config.url), {
requestInit: config.headers ? { headers: config.headers } : undefined,
authProvider,
@@ -202,13 +249,57 @@ export const connect = Effect.fnUntraced(function* (
}).pipe(Effect.exit)
if (Exit.isSuccess(exit)) {
yield* Effect.addFinalizer(() =>
cleanupStdioDescendants(transport).pipe(
Effect.andThen(Effect.promise(() => client.close())),
Effect.ignore,
),
cleanupStdioDescendants(transport).pipe(Effect.andThen(Effect.promise(() => client.close())), Effect.ignore),
)
const catalogTimeout = config.timeout?.catalog ?? DEFAULT_CATALOG_TIMEOUT
const executionTimeout = config.timeout?.execution ?? DEFAULT_EXECUTION_TIMEOUT
const resourceSubscriptions = new Map<string, Set<() => void>>()
const resourceListChangedCallbacks = new Set<() => void>()
const resourceSubscriptionLock = Semaphore.makeUnsafe(1)
const notify = async (callbacks: Iterable<() => void>) => {
for (const callback of callbacks) {
try {
await callback()
} catch {}
}
}
client.setNotificationHandler(ResourceUpdatedNotificationSchema, (notification) =>
notify(resourceSubscriptions.get(notification.params.uri) ?? []),
)
client.setNotificationHandler(ResourceListChangedNotificationSchema, () => notify(resourceListChangedCallbacks))
if (transport instanceof StreamableHTTPClientTransport) {
const recover = transport.onsessionexpired
transport.onsessionexpired = async () => {
await recover?.()
await notify(resourceListChangedCallbacks)
if (client.getServerCapabilities()?.resources?.subscribe)
// The transport cannot send ordinary requests until its recovery callback returns.
setTimeout(() => {
Effect.runFork(
resourceSubscriptionLock.withPermit(
Effect.forEach(
resourceSubscriptions.keys(),
(uri) =>
Effect.tryPromise({
try: (signal) => client.subscribeResource({ uri }, { signal, timeout: catalogTimeout }),
catch: (error) => (error instanceof Error ? error : new Error(String(error))),
}).pipe(
Effect.tapError((error) =>
Effect.logWarning("failed to restore MCP resource subscription", {
server,
uri,
error: error.message,
}),
),
Effect.ignore,
),
{ discard: true },
),
),
)
}, 0)
}
}
return {
instructions: client.getInstructions()?.trim() || undefined,
tools: () =>
@@ -257,7 +348,9 @@ export const connect = Effect.fnUntraced(function* (
),
catch: (error) => (error instanceof Error ? error : new Error(String(error))),
}).pipe(
Effect.tapError((error) => Effect.logWarning("failed to list MCP prompts", { server, error: error.message })),
Effect.tapError((error) =>
Effect.logWarning("failed to list MCP prompts", { server, error: error.message }),
),
)
return prompts.map((prompt) => ({
name: prompt.name,
@@ -269,6 +362,127 @@ export const connect = Effect.fnUntraced(function* (
})),
}))
}),
resources: () =>
Effect.gen(function* () {
if (!client.getServerCapabilities()?.resources) return []
const resources = yield* Effect.tryPromise({
try: () =>
paginate(
(cursor) =>
client.listResources(cursor === undefined ? undefined : { cursor }, { timeout: catalogTimeout }),
(result) => result.resources,
),
catch: (error) => (error instanceof Error ? error : new Error(String(error))),
}).pipe(
Effect.tapError((error) =>
Effect.logWarning("failed to list MCP resources", { server, error: error.message }),
),
)
return resources.map((resource) => ({
name: resource.name,
uri: resource.uri,
description: resource.description,
mimeType: resource.mimeType,
}))
}),
resourceTemplates: () =>
Effect.gen(function* () {
if (!client.getServerCapabilities()?.resources) return []
const templates = yield* Effect.tryPromise({
try: () =>
paginate(
(cursor) =>
client.listResourceTemplates(cursor === undefined ? undefined : { cursor }, {
timeout: catalogTimeout,
}),
(result) => result.resourceTemplates,
),
catch: (error) => (error instanceof Error ? error : new Error(String(error))),
}).pipe(
Effect.tapError((error) =>
Effect.logWarning("failed to list MCP resource templates", { server, error: error.message }),
),
)
return templates.map((template) => ({
name: template.name,
uriTemplate: template.uriTemplate,
description: template.description,
mimeType: template.mimeType,
}))
}),
readResource: (input) =>
Effect.gen(function* () {
if (!client.getServerCapabilities()?.resources) return undefined
const result = yield* Effect.tryPromise({
try: (signal) => client.readResource({ uri: input.uri }, { signal, timeout: executionTimeout }),
catch: (error) => (error instanceof Error ? error : new Error(String(error))),
}).pipe(
Effect.tapError((error) =>
Effect.logWarning("failed to read MCP resource", { server, uri: input.uri, error: error.message }),
),
)
return {
contents: result.contents.map(
(part): ResourceContentPart =>
"text" in part
? { type: "text", uri: part.uri, text: part.text, mimeType: part.mimeType }
: { type: "blob", uri: part.uri, blob: part.blob, mimeType: part.mimeType },
),
}
}),
subscribeResource: (input, callback) =>
Effect.acquireRelease(
resourceSubscriptionLock.withPermit(
Effect.gen(function* () {
if (!client.getServerCapabilities()?.resources?.subscribe) return undefined
const listener = () => callback()
const current = resourceSubscriptions.get(input.uri)
if (current) current.add(listener)
if (!current) {
yield* Effect.tryPromise({
try: (signal) => client.subscribeResource({ uri: input.uri }, { signal, timeout: catalogTimeout }),
catch: (error) => (error instanceof Error ? error : new Error(String(error))),
}).pipe(
Effect.tapError((error) =>
Effect.logWarning("failed to subscribe to MCP resource", {
server,
uri: input.uri,
error: error.message,
}),
),
)
resourceSubscriptions.set(input.uri, new Set([listener]))
}
return listener
}),
),
(listener) => {
if (!listener) return Effect.void
return resourceSubscriptionLock.withPermit(
Effect.gen(function* () {
const listeners = resourceSubscriptions.get(input.uri)
if (!listeners) return
listeners.delete(listener)
if (listeners.size > 0) return
resourceSubscriptions.delete(input.uri)
yield* Effect.tryPromise({
try: (signal) => client.unsubscribeResource({ uri: input.uri }, { signal, timeout: catalogTimeout }),
catch: (error) => (error instanceof Error ? error : new Error(String(error))),
}).pipe(
Effect.tapError((error) =>
Effect.logWarning("failed to unsubscribe from MCP resource", {
server,
uri: input.uri,
error: error.message,
}),
),
Effect.ignore,
)
}),
)
},
{ interruptible: true },
).pipe(Effect.map((listener) => listener !== undefined)),
prompt: (input) =>
Effect.tryPromise({
try: (signal) =>
@@ -315,7 +529,10 @@ export const connect = Effect.fnUntraced(function* (
})),
),
onClose: (callback) => {
client.onclose = callback
client.onclose = () => {
resourceSubscriptions.clear()
callback()
}
},
onLog: (callback) => {
client.setNotificationHandler(LoggingMessageNotificationSchema, (notification) => callback(notification.params))
@@ -328,13 +545,13 @@ export const connect = Effect.fnUntraced(function* (
if (!client.getServerCapabilities()?.prompts?.listChanged) return
client.setNotificationHandler(PromptListChangedNotificationSchema, async () => callback())
},
onResourcesChanged: (callback) => {
resourceListChangedCallbacks.add(callback)
},
} satisfies Connection
}
yield* cleanupStdioDescendants(transport).pipe(
Effect.andThen(Effect.promise(() => transport.close())),
Effect.ignore,
)
yield* cleanupStdioDescendants(transport).pipe(Effect.andThen(Effect.promise(() => transport.close())), Effect.ignore)
const error = Cause.squash(exit.cause)
if (error instanceof UnauthorizedError) return yield* new NeedsAuthError({ server })
return yield* new ConnectError({ server, message: error instanceof Error ? error.message : String(error) })
+105 -50
View File
@@ -83,48 +83,16 @@ export class PromptResult extends Schema.Class<PromptResult>("MCP.PromptResult")
messages: Schema.Array(PromptMessage),
}) {}
export class Resource extends Schema.Class<Resource>("MCP.Resource")({
server: ServerName,
name: Schema.String,
uri: Schema.String,
description: Schema.String.pipe(Schema.optional),
mimeType: Schema.String.pipe(Schema.optional),
}) {}
export class ResourceTemplate extends Schema.Class<ResourceTemplate>("MCP.ResourceTemplate")({
server: ServerName,
name: Schema.String,
uriTemplate: Schema.String,
description: Schema.String.pipe(Schema.optional),
mimeType: Schema.String.pipe(Schema.optional),
}) {}
export class ResourceCatalog extends Schema.Class<ResourceCatalog>("MCP.ResourceCatalog")({
resources: Schema.Array(Resource),
templates: Schema.Array(ResourceTemplate),
}) {}
export const ResourceContentPart = Schema.Union([
Schema.Struct({
type: Schema.Literal("text"),
uri: Schema.String,
text: Schema.String,
mimeType: Schema.String.pipe(Schema.optional),
}),
Schema.Struct({
type: Schema.Literal("blob"),
uri: Schema.String,
blob: Schema.String,
mimeType: Schema.String.pipe(Schema.optional),
}),
]).pipe(Schema.toTaggedUnion("type"))
export type ResourceContentPart = typeof ResourceContentPart.Type
export class ResourceContent extends Schema.Class<ResourceContent>("MCP.ResourceContent")({
server: ServerName,
uri: Schema.String,
contents: Schema.Array(ResourceContentPart),
}) {}
export const Resource = Mcp.Resource
export type Resource = Mcp.Resource
export const ResourceTemplate = Mcp.ResourceTemplate
export type ResourceTemplate = Mcp.ResourceTemplate
export const ResourceCatalog = Mcp.ResourceCatalog
export type ResourceCatalog = Mcp.ResourceCatalog
export const ResourceContentPart = Mcp.ResourceContentPart
export type ResourceContentPart = Mcp.ResourceContentPart
export const ResourceContent = Mcp.ResourceContent
export type ResourceContent = Mcp.ResourceContent
export class NotFoundError extends Schema.TaggedErrorClass<NotFoundError>()("MCP.NotFoundError", {
server: ServerName,
@@ -148,6 +116,9 @@ type ServerEntry = {
client?: MCPClient.Connection
tools?: ReadonlyArray<Tool>
prompts?: ReadonlyArray<Prompt>
resources?: ReadonlyArray<Resource>
resourceTemplates?: ReadonlyArray<ResourceTemplate>
resourceRevision: number
// Set when a remote server is registered as an OAuth integration; the credential lives in the global store.
integrationID?: Integration.ID
}
@@ -208,6 +179,7 @@ export const layer = Layer.effect(
config: { ...server, timeout: { ...timeout, ...server.timeout } },
status: { status: "pending" },
startup: Deferred.makeUnsafe<void>(),
resourceRevision: 0,
})
}
}
@@ -415,6 +387,55 @@ export const layer = Layer.effect(
),
})
const toResource = (server: ServerName, def: MCPClient.ResourceDefinition) =>
Resource.make({
server,
name: def.name,
uri: def.uri,
description: def.description,
mimeType: def.mimeType,
})
const toResourceTemplate = (server: ServerName, def: MCPClient.ResourceTemplateDefinition) =>
ResourceTemplate.make({
server,
name: def.name,
uriTemplate: def.uriTemplate,
description: def.description,
mimeType: def.mimeType,
})
const invalidateResources = (entry: ServerEntry) => {
entry.resourceRevision += 1
entry.resources = undefined
entry.resourceTemplates = undefined
}
const loadResources = Effect.fnUntraced(function* (
name: ServerName,
entry: ServerEntry,
connection: MCPClient.Connection,
) {
const revision = entry.resourceRevision
const result = yield* Effect.all(
{
resources:
entry.resources === undefined
? connection.resources().pipe(Effect.catch(() => Effect.succeed(undefined)))
: Effect.succeed(undefined),
templates:
entry.resourceTemplates === undefined
? connection.resourceTemplates().pipe(Effect.catch(() => Effect.succeed(undefined)))
: Effect.succeed(undefined),
},
{ concurrency: "unbounded" },
)
if (entry.client !== connection || entry.resourceRevision !== revision) return
if (result.resources !== undefined) entry.resources = result.resources.map((def) => toResource(name, def))
if (result.templates !== undefined)
entry.resourceTemplates = result.templates.map((def) => toResourceTemplate(name, def))
})
const refreshTools = (name: ServerName, entry: ServerEntry, connection: MCPClient.Connection) =>
connection.tools().pipe(
Effect.map((defs) => {
@@ -441,8 +462,10 @@ export const layer = Layer.effect(
entry.client = undefined
entry.tools = undefined
entry.prompts = undefined
invalidateResources(entry)
entry.status = { status: "failed", error: "Connection closed" }
fork(events.publish(McpEvent.ToolsChanged, { server: name }).pipe(Effect.ignore))
fork(events.publish(McpEvent.ResourcesChanged, { server: name }).pipe(Effect.ignore))
fork(events.publish(Command.Event.Updated, {}).pipe(Effect.ignore))
fork(events.publish(McpEvent.StatusChanged, { server: name }).pipe(Effect.ignore))
})
@@ -458,6 +481,11 @@ export const layer = Layer.effect(
connection.onPromptsChanged(() => {
fork(refreshPrompts(name, entry, connection).pipe(Effect.ignore))
})
connection.onResourcesChanged(() => {
if (entry.client !== connection) return
invalidateResources(entry)
fork(events.publish(McpEvent.ResourcesChanged, { server: name }).pipe(Effect.ignore))
})
}
const serverLog = (server: ServerName, message: MCPClient.LogMessage) => {
@@ -491,6 +519,7 @@ export const layer = Layer.effect(
Effect.exit,
)
if (Exit.isSuccess(result)) {
invalidateResources(entry)
entry.client = result.value.connection
entry.tools = result.value.tools.map((def) => toTool(name, def))
entry.prompts = []
@@ -501,6 +530,7 @@ export const layer = Layer.effect(
// after the initial registration sweep and emits no list-changed notification would otherwise
// stay invisible to the model.
yield* events.publish(McpEvent.ToolsChanged, { server: name }).pipe(Effect.ignore)
yield* events.publish(McpEvent.ResourcesChanged, { server: name }).pipe(Effect.ignore)
yield* events.publish(McpEvent.StatusChanged, { server: name }).pipe(Effect.ignore)
fork(refreshPrompts(name, entry, result.value.connection).pipe(Effect.ignore))
return
@@ -541,6 +571,7 @@ export const layer = Layer.effect(
entry.client = undefined
entry.tools = undefined
entry.prompts = undefined
invalidateResources(entry)
yield* events.publish(Command.Event.Updated, {}).pipe(Effect.ignore)
}
yield* startServer(name, entry)
@@ -557,11 +588,6 @@ export const layer = Layer.effect(
concurrency: "unbounded",
discard: true,
})
const gate = Effect.fnUntraced(function* (server: ServerName | string) {
const target = yield* requireServer(server)
yield* Deferred.await(target.entry.startup)
})
return Service.of({
servers: Effect.fn("MCP.servers")(function* () {
const entries = Array.from(runtime).toSorted(([a], [b]) => a.localeCompare(b))
@@ -637,11 +663,40 @@ export const layer = Layer.effect(
}),
resourceCatalog: Effect.fn("MCP.resourceCatalog")(function* () {
yield* whenAllReady
return new ResourceCatalog({ resources: [], templates: [] })
yield* Effect.forEach(
runtime,
([name, entry]) => (entry.client ? loadResources(name, entry, entry.client) : Effect.void),
{ concurrency: "unbounded", discard: true },
)
return ResourceCatalog.make({
resources: Array.from(runtime.values())
.flatMap((entry) => entry.resources ?? [])
.toSorted(
(a, b) => a.server.localeCompare(b.server) || a.name.localeCompare(b.name) || a.uri.localeCompare(b.uri),
),
templates: Array.from(runtime.values())
.flatMap((entry) => entry.resourceTemplates ?? [])
.toSorted(
(a, b) =>
a.server.localeCompare(b.server) ||
a.name.localeCompare(b.name) ||
a.uriTemplate.localeCompare(b.uriTemplate),
),
})
}),
readResource: Effect.fn("MCP.readResource")(function* (input) {
yield* gate(input.server)
return undefined
const target = yield* requireServer(input.server)
yield* Deferred.await(target.entry.startup)
if (!target.entry.client) return undefined
const result = yield* target.entry.client
.readResource({ uri: input.uri })
.pipe(Effect.catch(() => Effect.succeed(undefined)))
if (!result) return undefined
return ResourceContent.make({
server: target.name,
uri: input.uri,
contents: result.contents,
})
}),
})
}),
+1 -15
View File
@@ -43,20 +43,15 @@ export interface Registration {
readonly group?: string
}
export interface CodeModeTools {
[name: string]: Tool.Definition<never> | CodeModeTools
}
export const create = (options: {
readonly registrations: ReadonlyMap<string, Registration>
readonly current: (name: string) => Registration | undefined
readonly tools: CodeModeTools
}) => {
const runtime = (
invoke: (name: string, registration: Registration, input: unknown) => Effect.Effect<unknown, unknown>,
hooks?: CodeMode.ToolCallHooks,
) => {
const tools = cloneTools(options.tools)
const tools: Record<string, Tool.Definition<never> | Record<string, Tool.Definition<never>>> = {}
for (const [name, registration] of options.registrations) {
const child = definition(name, registration.tool)
const value = Tool.make({
@@ -168,15 +163,6 @@ export const create = (options: {
})
}
function cloneTools(tools: CodeModeTools): CodeModeTools {
return Object.assign(
Object.create(null),
Object.fromEntries(
Object.entries(tools).map(([name, value]) => [name, Tool.isDefinition(value) ? value : cloneTools(value)]),
),
)
}
function displayInput(input: unknown): Record<string, unknown> | undefined {
if (input === null || input === undefined) return
if (typeof input !== "object" || Array.isArray(input)) return { input }
+4 -22
View File
@@ -1,5 +1,4 @@
export * as ToolRegistry from "./registry"
export type { CodeModeTools } from "./execute"
import { ToolOutput, type ToolCall, type ToolDefinition, type ToolResultValue } from "@opencode-ai/llm"
import { Context, Effect, Layer, Scope } from "effect"
@@ -10,12 +9,11 @@ import { SessionMessage } from "../session/message"
import { SessionSchema } from "../session/schema"
import { ToolOutputStore } from "../tool-output-store"
import { Wildcard } from "../util/wildcard"
import { ExecuteTool, type CodeModeTools } from "./execute"
import { ExecuteTool } from "./execute"
import { definition, permission, registrationEntries, RegistrationError, settle, type AnyTool } from "./tool"
import { Tools } from "./tools"
import { ToolHooks } from "./hooks"
import { makeLocationNode } from "../effect/app-node"
import { LayerNode } from "../effect/layer-node"
import { SessionError } from "@opencode-ai/schema/session-error"
import { toSessionError } from "../session/to-session-error"
@@ -53,20 +51,10 @@ export interface Settlement {
}
export class Service extends Context.Service<Service, Interface>()("@opencode/v2/ToolRegistry") {}
class CodeModeCatalog extends Context.Service<CodeModeCatalog, { readonly tools: CodeModeTools }>()(
"@opencode/v2/CodeModeCatalog",
) {}
const codeModeCatalogNode = makeLocationNode({
service: CodeModeCatalog,
layer: Layer.succeed(CodeModeCatalog, CodeModeCatalog.of({ tools: {} })),
deps: [],
})
const registryLayer = Layer.effect(
Service,
Effect.gen(function* () {
const codeModeTools = (yield* CodeModeCatalog).tools
const resources = yield* ToolOutputStore.Service
const toolHooks = yield* ToolHooks.Service
type Registration = {
@@ -216,13 +204,11 @@ const registryLayer = Layer.effect(
}
const direct = new Map(Array.from(registrations).filter(([, registration]) => !registration.deferred))
const deferred = new Map(Array.from(registrations).filter(([, registration]) => registration.deferred))
const tools = Flag.CODEMODE_ENABLED ? codeModeTools : {}
const execute =
(deferred.size > 0 || Object.keys(tools).length > 0) && !whollyDisabled("execute", input.permissions ?? [])
deferred.size > 0 && !whollyDisabled("execute", input.permissions ?? [])
? ExecuteTool.create({
registrations: deferred,
current: (name) => local.get(name)?.at(-1)?.registration,
tools,
})
: undefined
return {
@@ -255,18 +241,14 @@ function whollyDisabled(action: string, rules: PermissionV2.Ruleset) {
return rule?.resource === "*" && rule.effect === "deny"
}
export function codeModeReplacement(tools: CodeModeTools): LayerNode.Replacement {
return [codeModeCatalogNode, Layer.succeed(CodeModeCatalog, CodeModeCatalog.of({ tools }))]
}
export const node = makeLocationNode({
service: Service,
layer,
deps: [ToolOutputStore.node, ToolHooks.node, codeModeCatalogNode],
deps: [ToolOutputStore.node, ToolHooks.node],
})
export const toolsNode = makeLocationNode({
service: Tools.Service,
layer,
deps: [ToolOutputStore.node, ToolHooks.node, codeModeCatalogNode],
deps: [ToolOutputStore.node, ToolHooks.node],
})
+16 -1
View File
@@ -4,16 +4,27 @@ import {
CallToolRequestSchema,
GetPromptRequestSchema,
ListPromptsRequestSchema,
ListResourcesRequestSchema,
ListResourceTemplatesRequestSchema,
ListToolsRequestSchema,
ReadResourceRequestSchema,
} from "@modelcontextprotocol/sdk/types.js"
const server = new Server({ name: "timeout", version: "1.0.0" }, { capabilities: { prompts: {}, tools: {} } })
const server = new Server(
{ name: "timeout", version: "1.0.0" },
{ capabilities: { prompts: {}, resources: {}, tools: {} } },
)
server.setRequestHandler(ListToolsRequestSchema, async () => {
if (process.env.MCP_TIMEOUT_TARGET === "catalog") await Bun.sleep(100)
return { tools: [{ name: "slow", inputSchema: { type: "object" } }] }
})
server.setRequestHandler(ListPromptsRequestSchema, () => Promise.resolve({ prompts: [{ name: "slow" }] }))
server.setRequestHandler(ListResourcesRequestSchema, async () => {
if (process.env.MCP_TIMEOUT_TARGET === "resource-catalog") await Bun.sleep(100)
return { resources: [{ name: "slow", uri: "test://slow" }] }
})
server.setRequestHandler(ListResourceTemplatesRequestSchema, () => Promise.resolve({ resourceTemplates: [] }))
server.setRequestHandler(CallToolRequestSchema, async () => {
await Bun.sleep(100)
return { content: [] }
@@ -22,5 +33,9 @@ server.setRequestHandler(GetPromptRequestSchema, async () => {
await Bun.sleep(100)
return { messages: [] }
})
server.setRequestHandler(ReadResourceRequestSchema, async (request) => {
await Bun.sleep(100)
return { contents: [{ uri: request.params.uri, text: "slow" }] }
})
await server.connect(new StdioServerTransport())
+2 -5
View File
@@ -14,15 +14,12 @@ export const emptyMcpLayer = Layer.succeed(
instructions: () => Effect.succeed([]),
prompts: () => Effect.succeed([]),
prompt: () => Effect.succeed(undefined),
resourceCatalog: () => Effect.succeed(new MCP.ResourceCatalog({ resources: [], templates: [] })),
resourceCatalog: () => Effect.succeed(MCP.ResourceCatalog.make({ resources: [], templates: [] })),
readResource: () => Effect.succeed(undefined),
}),
)
export const emptyConfigLayer = Layer.succeed(
Config.Service,
Config.Service.of({ entries: () => Effect.succeed([]) }),
)
export const emptyConfigLayer = Layer.succeed(Config.Service, Config.Service.of({ entries: () => Effect.succeed([]) }))
export const testLocationLayer = Layer.succeed(
Location.Service,
+470 -2
View File
@@ -3,26 +3,203 @@ import { describe, expect, test } from "bun:test"
import { Client } from "@modelcontextprotocol/sdk/client/index.js"
import { InMemoryTransport } from "@modelcontextprotocol/sdk/inMemory.js"
import { Server } from "@modelcontextprotocol/sdk/server/index.js"
import { CallToolRequestSchema, ListToolsRequestSchema } from "@modelcontextprotocol/sdk/types.js"
import { WebStandardStreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/webStandardStreamableHttp.js"
import {
CallToolRequestSchema,
ListResourcesRequestSchema,
ListResourceTemplatesRequestSchema,
ListToolsRequestSchema,
ReadResourceRequestSchema,
SubscribeRequestSchema,
UnsubscribeRequestSchema,
} from "@modelcontextprotocol/sdk/types.js"
import { ConfigMCP } from "@opencode-ai/core/config/mcp"
import { Config } from "@opencode-ai/core/config"
import { Credential } from "@opencode-ai/core/credential"
import { AppNodeBuilder } from "@opencode-ai/core/effect/app-node-builder"
import { LayerNode } from "@opencode-ai/core/effect/layer-node"
import { EventV2 } from "@opencode-ai/core/event"
import { Form } from "@opencode-ai/core/form"
import { Integration } from "@opencode-ai/core/integration"
import { Location } from "@opencode-ai/core/location"
import { MCP } from "@opencode-ai/core/mcp/index"
import { MCPClient } from "@opencode-ai/core/mcp/client"
import { PermissionV2 } from "@opencode-ai/core/permission"
import { AbsolutePath } from "@opencode-ai/core/schema"
import { SessionV2 } from "@opencode-ai/core/session"
import { McpTool } from "@opencode-ai/core/tool/mcp"
import { ToolRegistry } from "@opencode-ai/core/tool/registry"
import { ToolOutputStore } from "@opencode-ai/core/tool-output-store"
import { Deferred, Effect, Fiber, Layer, Stream } from "effect"
import { Deferred, Effect, Exit, Fiber, Layer, Scope, Stream } from "effect"
import { testEffect } from "./lib/effect"
import { location } from "./fixture/location"
import { settleTool, toolDefinitions, toolIdentity, waitForTool } from "./lib/tool"
let assertion: Deferred.Deferred<PermissionV2.AssertInput> | undefined
let decision: Effect.Effect<void, PermissionV2.Error> = Effect.void
let calls = 0
type ResourcePage = {
items: Array<{ name: string; uri: string; description?: string; mimeType?: string }>
nextCursor?: string
}
type ResourceTemplatePage = {
items: Array<{ name: string; uriTemplate: string; description?: string; mimeType?: string }>
nextCursor?: string
}
function resourceServer(input: { resources?: boolean; subscribe?: boolean; listChanged?: boolean } = {}) {
return Effect.acquireRelease(
Effect.promise(async () => {
const state = {
resources: [] as ResourcePage["items"],
templates: [] as ResourceTemplatePage["items"],
resourcePages: undefined as Record<string, ResourcePage> | undefined,
templatePages: undefined as Record<string, ResourceTemplatePage> | undefined,
contents: [
{ uri: "docs://readme", text: "hello", mimeType: "text/plain" },
{ uri: "docs://logo", blob: "aGVsbG8=", mimeType: "image/png" },
] as Array<{ uri: string; text: string; mimeType?: string } | { uri: string; blob: string; mimeType?: string }>,
resourceLists: 0,
templateLists: 0,
resourceListFailures: 0,
subscriptions: [] as string[],
unsubscriptions: [] as string[],
onSubscription: undefined as (() => void) | undefined,
}
const makeProtocol = async () => {
const protocol = new Server(
{ name: "mcp-resources", version: "1.0.0" },
{
capabilities: {
tools: {},
...(input.resources === false
? {}
: { resources: { subscribe: input.subscribe, listChanged: input.listChanged } }),
},
},
)
protocol.setRequestHandler(ListToolsRequestSchema, () => Promise.resolve({ tools: [] }))
if (input.resources !== false) {
protocol.setRequestHandler(ListResourcesRequestSchema, (request) => {
state.resourceLists += 1
if (state.resourceListFailures > 0) {
state.resourceListFailures -= 1
throw new Error("resource list failed")
}
const page = state.resourcePages?.[request.params?.cursor ?? "initial"]
return Promise.resolve({ resources: page?.items ?? state.resources, nextCursor: page?.nextCursor })
})
protocol.setRequestHandler(ListResourceTemplatesRequestSchema, (request) => {
state.templateLists += 1
const page = state.templatePages?.[request.params?.cursor ?? "initial"]
return Promise.resolve({ resourceTemplates: page?.items ?? state.templates, nextCursor: page?.nextCursor })
})
protocol.setRequestHandler(ReadResourceRequestSchema, () => Promise.resolve({ contents: state.contents }))
protocol.setRequestHandler(SubscribeRequestSchema, (request) => {
state.subscriptions.push(request.params.uri)
state.onSubscription?.()
return Promise.resolve({})
})
protocol.setRequestHandler(UnsubscribeRequestSchema, (request) => {
state.unsubscriptions.push(request.params.uri)
return Promise.resolve({})
})
}
const transport = new WebStandardStreamableHTTPServerTransport({
sessionIdGenerator: () => crypto.randomUUID(),
enableJsonResponse: true,
})
await protocol.connect(transport)
return { protocol, transport }
}
let current = await makeProtocol()
let expireSession = false
const http = Bun.serve({
port: 0,
fetch: (request) => {
if (expireSession && request.headers.has("mcp-session-id")) {
expireSession = false
return new Response("session expired", { status: 404 })
}
return current.transport.handleRequest(request)
},
})
return {
state,
url: http.url.toString(),
sendResourceListChanged: () => current.protocol.sendResourceListChanged(),
sendResourceUpdated: (uri: string) => current.protocol.sendResourceUpdated({ uri }),
restart: async () => {
current = await makeProtocol()
expireSession = true
},
close: async () => {
await current.protocol.close().catch(() => {})
http.stop(true)
},
}
}),
(server) => Effect.promise(server.close),
)
}
function resourceMcpLayer(url: string, changed: Deferred.Deferred<void>) {
const directory = AbsolutePath.make(import.meta.dir)
const unusedIntegration = () => Effect.die("unused integration service")
let resourceChanges = 0
return MCP.layer.pipe(
Layer.provide(
Layer.mergeAll(
Layer.succeed(
Config.Service,
Config.Service.of({
entries: () =>
Effect.succeed([
new Config.Document({
type: "document",
info: new Config.Info({
mcp: new ConfigMCP.Info({
servers: { resources: new ConfigMCP.Remote({ type: "remote", url, oauth: false }) },
}),
}),
}),
]),
}),
),
Layer.succeed(Location.Service, Location.Service.of(location({ directory }))),
Layer.mock(EventV2.Service, {
subscribe: () => Stream.never,
publish: (definition) =>
Effect.sync(() => {
if (definition.type === "mcp.resources.changed" && ++resourceChanges === 2)
Deferred.doneUnsafe(changed, Exit.void)
return undefined as never
}),
}),
Layer.mock(Form.Service, {}),
Layer.mock(Integration.Service, {
connection: {
active: unusedIntegration,
resolve: unusedIntegration,
key: unusedIntegration,
oauth: unusedIntegration,
update: unusedIntegration,
remove: unusedIntegration,
},
attempt: {
status: unusedIntegration,
complete: unusedIntegration,
cancel: unusedIntegration,
},
}),
Layer.mock(Credential.Service, {}),
),
),
)
}
const mcp = Layer.mock(MCP.Service, {
tools: () =>
Effect.succeed([
@@ -241,6 +418,297 @@ test("applies the configured MCP execution timeout to prompts", async () => {
await expect(result).rejects.toThrow("Request timed out")
})
test("applies configured MCP timeouts to resource operations", async () => {
const catalog = Effect.runPromise(
Effect.scoped(
Effect.gen(function* () {
const connection = yield* MCPClient.connect(
"resource-catalog-timeout",
new ConfigMCP.Local({
type: "local",
command: [process.execPath, path.join(import.meta.dir, "fixture/mcp-timeout.ts")],
environment: { MCP_TIMEOUT_TARGET: "resource-catalog" },
timeout: new ConfigMCP.Timeout({ catalog: 10 }),
}),
import.meta.dir,
)
return yield* connection.resources()
}),
),
)
await expect(catalog).rejects.toThrow("Request timed out")
const read = Effect.runPromise(
Effect.scoped(
Effect.gen(function* () {
const connection = yield* MCPClient.connect(
"resource-read-timeout",
new ConfigMCP.Local({
type: "local",
command: [process.execPath, path.join(import.meta.dir, "fixture/mcp-timeout.ts")],
timeout: new ConfigMCP.Timeout({ execution: 10 }),
}),
import.meta.dir,
)
return yield* connection.readResource({ uri: "test://slow" })
}),
),
)
await expect(read).rejects.toThrow("Request timed out")
})
test("lists, reads, and invalidates MCP resources", async () => {
await Effect.runPromise(
Effect.scoped(
Effect.gen(function* () {
const server = yield* resourceServer({ listChanged: true })
server.state.resourcePages = {
initial: {
items: [{ name: "Readme", uri: "docs://readme", description: "Project docs" }],
nextCursor: "resources-2",
},
"resources-2": { items: [{ name: "Logo", uri: "docs://logo", mimeType: "image/png" }] },
}
server.state.templatePages = {
initial: {
items: [{ name: "File", uriTemplate: "docs://{path}" }],
nextCursor: "templates-2",
},
"templates-2": { items: [{ name: "Issue", uriTemplate: "issue://{id}", description: "Issue" }] },
}
const connection = yield* MCPClient.connect(
"resources",
new ConfigMCP.Remote({ type: "remote", url: server.url, oauth: false }),
import.meta.dir,
)
expect(yield* connection.resources()).toEqual([
{ name: "Readme", uri: "docs://readme", description: "Project docs", mimeType: undefined },
{ name: "Logo", uri: "docs://logo", description: undefined, mimeType: "image/png" },
])
expect(yield* connection.resourceTemplates()).toEqual([
{ name: "File", uriTemplate: "docs://{path}", description: undefined, mimeType: undefined },
{ name: "Issue", uriTemplate: "issue://{id}", description: "Issue", mimeType: undefined },
])
expect(yield* connection.readResource({ uri: "docs://readme" })).toEqual({
contents: [
{ type: "text", uri: "docs://readme", text: "hello", mimeType: "text/plain" },
{ type: "blob", uri: "docs://logo", blob: "aGVsbG8=", mimeType: "image/png" },
],
})
const changed = yield* Deferred.make<void>()
connection.onResourcesChanged(() => Deferred.doneUnsafe(changed, Exit.void))
yield* Effect.promise(server.sendResourceListChanged)
yield* Deferred.await(changed)
}),
),
)
})
test("shares scoped MCP resource subscriptions", async () => {
await Effect.runPromise(
Effect.scoped(
Effect.gen(function* () {
const server = yield* resourceServer({ subscribe: true })
const connection = yield* MCPClient.connect(
"resources",
new ConfigMCP.Remote({ type: "remote", url: server.url, oauth: false }),
import.meta.dir,
)
const firstScope = yield* Scope.make()
const secondScope = yield* Scope.make()
const secondUpdate = yield* Deferred.make<void>()
expect(
yield* connection
.subscribeResource({ uri: "docs://readme" }, () => {
throw new Error("listener failed")
})
.pipe(Scope.provide(firstScope)),
).toBe(true)
expect(
yield* connection
.subscribeResource({ uri: "docs://readme" }, () => Deferred.doneUnsafe(secondUpdate, Exit.void))
.pipe(Scope.provide(secondScope)),
).toBe(true)
expect(server.state.subscriptions).toEqual(["docs://readme"])
yield* Effect.promise(() => server.sendResourceUpdated("docs://readme"))
yield* Deferred.await(secondUpdate)
yield* Scope.close(firstScope, Exit.void)
expect(server.state.unsubscriptions).toEqual([])
yield* Scope.close(secondScope, Exit.void)
expect(server.state.unsubscriptions).toEqual(["docs://readme"])
}),
),
)
})
test("releases MCP resource subscriptions provided a closed scope", async () => {
await Effect.runPromise(
Effect.scoped(
Effect.gen(function* () {
const server = yield* resourceServer({ subscribe: true })
const connection = yield* MCPClient.connect(
"resources",
new ConfigMCP.Remote({ type: "remote", url: server.url, oauth: false }),
import.meta.dir,
)
const closed = yield* Scope.make()
yield* Scope.close(closed, Exit.void)
expect(
yield* connection
.subscribeResource({ uri: "docs://readme" }, () => {})
.pipe(
Scope.provide(closed),
Effect.timeoutOrElse({
duration: "1 second",
orElse: () => Effect.fail(new Error("closed-scope resource subscription deadlocked")),
}),
),
).toBe(true)
expect(server.state.subscriptions).toEqual(["docs://readme"])
expect(server.state.unsubscriptions).toEqual(["docs://readme"])
}),
),
)
})
test("restores MCP resource state after HTTP session recovery", async () => {
await Effect.runPromise(
Effect.scoped(
Effect.gen(function* () {
const server = yield* resourceServer({ subscribe: true, listChanged: true })
server.state.resources = [{ name: "Readme", uri: "docs://readme" }]
const connection = yield* MCPClient.connect(
"resources",
new ConfigMCP.Remote({ type: "remote", url: server.url, oauth: false }),
import.meta.dir,
)
const changed = yield* Deferred.make<void>()
const subscriptionScope = yield* Scope.make()
connection.onResourcesChanged(() => Deferred.doneUnsafe(changed, Exit.void))
expect(
yield* connection
.subscribeResource({ uri: "docs://readme" }, () => {})
.pipe(Scope.provide(subscriptionScope)),
).toBe(true)
yield* Effect.promise(server.restart)
server.state.resources = [{ name: "Guide", uri: "docs://guide" }]
const resubscribed = yield* Deferred.make<void>()
server.state.onSubscription = () => Deferred.doneUnsafe(resubscribed, Exit.void)
expect((yield* connection.resources()).map((resource) => resource.uri)).toEqual(["docs://guide"])
yield* Deferred.await(changed)
yield* Deferred.await(resubscribed)
expect(server.state.subscriptions).toEqual(["docs://readme", "docs://readme"])
yield* Scope.close(subscriptionScope, Exit.void)
}),
),
)
})
test("skips unsupported MCP resource subscriptions", async () => {
await Effect.runPromise(
Effect.scoped(
Effect.gen(function* () {
const server = yield* resourceServer()
const connection = yield* MCPClient.connect(
"resources",
new ConfigMCP.Remote({ type: "remote", url: server.url, oauth: false }),
import.meta.dir,
)
expect(yield* connection.subscribeResource({ uri: "docs://readme" }, () => {})).toBe(false)
expect(server.state.subscriptions).toEqual([])
}),
),
)
})
test("skips MCP resource requests when the capability is absent", async () => {
await Effect.runPromise(
Effect.scoped(
Effect.gen(function* () {
const server = yield* resourceServer({ resources: false })
const connection = yield* MCPClient.connect(
"resources",
new ConfigMCP.Remote({ type: "remote", url: server.url, oauth: false }),
import.meta.dir,
)
expect(yield* connection.resources()).toEqual([])
expect(yield* connection.resourceTemplates()).toEqual([])
expect(yield* connection.readResource({ uri: "docs://readme" })).toBeUndefined()
expect({ resources: server.state.resourceLists, templates: server.state.templateLists }).toEqual({
resources: 0,
templates: 0,
})
}),
),
)
})
test("caches and invalidates MCP resource catalogs", async () => {
await Effect.runPromise(
Effect.scoped(
Effect.gen(function* () {
const server = yield* resourceServer({ listChanged: true, subscribe: true })
server.state.resources = [{ name: "Readme", uri: "docs://readme" }]
server.state.templates = [{ name: "File", uriTemplate: "docs://{path}" }]
server.state.resourceListFailures = 1
const changed = yield* Deferred.make<void>()
yield* Effect.gen(function* () {
const service = yield* MCP.Service
expect(yield* service.resourceCatalog()).toEqual({
resources: [],
templates: [
{
server: "resources",
name: "File",
uriTemplate: "docs://{path}",
description: undefined,
mimeType: undefined,
},
],
})
expect((yield* service.resourceCatalog()).resources).toEqual([
{
server: "resources",
name: "Readme",
uri: "docs://readme",
description: undefined,
mimeType: undefined,
},
])
yield* service.resourceCatalog()
expect({ resources: server.state.resourceLists, templates: server.state.templateLists }).toEqual({
resources: 2,
templates: 1,
})
server.state.resources = [{ name: "Guide", uri: "docs://guide" }]
yield* Effect.promise(server.sendResourceListChanged)
yield* Deferred.await(changed)
expect((yield* service.resourceCatalog()).resources.map((resource) => resource.uri)).toEqual(["docs://guide"])
expect({ resources: server.state.resourceLists, templates: server.state.templateLists }).toEqual({
resources: 3,
templates: 2,
})
expect(yield* service.readResource({ server: "resources", uri: "docs://readme" })).toEqual({
server: "resources",
uri: "docs://readme",
contents: [
{ type: "text", uri: "docs://readme", text: "hello", mimeType: "text/plain" },
{ type: "blob", uri: "docs://logo", blob: "aGVsbG8=", mimeType: "image/png" },
],
})
}).pipe(Effect.provide(resourceMcpLayer(server.url, changed)))
}),
),
)
})
it.effect("advertises MCP output schemas to Code Mode", () =>
Effect.gen(function* () {
const registry = yield* ToolRegistry.Service
@@ -30,27 +30,6 @@ const outputStore = Layer.mock(ToolOutputStore.Service, {
})
const registryLayer = AppNodeBuilder.build(ToolRegistry.node, [[ToolOutputStore.node, outputStore]])
const it = testEffect(registryLayer)
const codeModeTools: ToolRegistry.CodeModeTools = {
opencode: {
v2: {
health: {
get: {
_tag: "CodeModeTool" as const,
description: "Get server health",
input: Schema.Struct({}),
output: Schema.Struct({ healthy: Schema.Boolean }),
run: () => Effect.succeed({ healthy: true }),
},
},
},
},
}
const codeModeIt = testEffect(
AppNodeBuilder.build(ToolRegistry.node, [
[ToolOutputStore.node, outputStore],
ToolRegistry.codeModeReplacement(codeModeTools),
]),
)
const identity = {
agent: AgentV2.ID.make("build"),
assistantMessageID: SessionMessage.ID.make("msg_registry"),
@@ -74,50 +53,6 @@ const make = (permission?: string) => {
}
describe("ToolRegistry", () => {
codeModeIt.effect("includes host Code Mode trees without hosted tool registration", () =>
Effect.gen(function* () {
const service = yield* ToolRegistry.Service
const definitions = yield* toolDefinitions(service)
expect(definitions.map((tool) => tool.name)).toEqual(["execute"])
expect(definitions[0]?.description).toContain("tools.opencode.v2.health.get")
expect(
yield* executeTool(service, {
sessionID,
...identity,
call: {
type: "tool-call",
id: "call-opencode-health",
name: "execute",
input: { code: "return await tools.opencode.v2.health.get({})" },
},
}),
).toEqual({ type: "text", value: '{\n "healthy": true\n}' })
}),
)
codeModeIt.effect("keeps host Code Mode trees immutable while merging deferred tools", () =>
Effect.gen(function* () {
const service = yield* ToolRegistry.Service
yield* service.register({ echo: make() }, { group: "opencode", deferred: true })
expect((yield* toolDefinitions(service))[0]?.description).toContain("tools.opencode.echo")
expect((yield* toolDefinitions(service))[0]?.description).toContain("tools.opencode.echo")
expect(
yield* executeTool(service, {
sessionID,
...identity,
call: {
type: "tool-call",
id: "call-opencode-echo",
name: "execute",
input: { code: 'return await tools.opencode.echo({ text: "hello" })' },
},
}),
).toEqual({ type: "text", value: '{\n "text": "hello"\n}' })
}),
)
it.effect("filters disabled tools with edit aliases and ordered wildcard precedence", () =>
Effect.gen(function* () {
const service = yield* ToolRegistry.Service
+2
View File
@@ -61,6 +61,8 @@ export const groupNames = {
} as const
export const endpointNames = {
"mcp.resource.catalog": "resourceCatalog",
"mcp.resource.read": "readResource",
"session.messages": "list",
"integration.connect.key": "connectKey",
"integration.connect.oauth": "connectOauth",
+9
View File
@@ -61,6 +61,15 @@ export class ProviderNotFoundError extends Schema.TaggedErrorClass<ProviderNotFo
{ httpApiStatus: 404 },
) {}
export class McpServerNotFoundError extends Schema.TaggedErrorClass<McpServerNotFoundError>()(
"McpServerNotFoundError",
{
server: Schema.String,
message: Schema.String,
},
{ httpApiStatus: 404 },
) {}
export class SessionNotFoundError extends Schema.TaggedErrorClass<SessionNotFoundError>()(
"SessionNotFoundError",
{
+32 -1
View File
@@ -2,6 +2,7 @@ import { Mcp } from "@opencode-ai/schema/mcp"
import { Location } from "@opencode-ai/schema/location"
import { Schema } from "effect"
import { HttpApiEndpoint, HttpApiGroup, OpenApi } from "effect/unstable/httpapi"
import { McpServerNotFoundError } from "../errors.js"
import { LocationQuery, locationQueryOpenApi } from "./location.js"
export const McpGroup = HttpApiGroup.make("server.mcp")
@@ -19,4 +20,34 @@ export const McpGroup = HttpApiGroup.make("server.mcp")
}),
),
)
.annotateMerge(OpenApi.annotations({ title: "mcp", description: "MCP server status routes." }))
.add(
HttpApiEndpoint.get("mcp.resource.catalog", "/api/mcp/resource", {
query: LocationQuery,
success: Location.response(Mcp.ResourceCatalog),
})
.annotateMerge(locationQueryOpenApi)
.annotateMerge(
OpenApi.annotations({
identifier: "v2.mcp.resource.catalog",
summary: "List MCP resources",
description: "Retrieve resources and resource templates from connected MCP servers.",
}),
),
)
.add(
HttpApiEndpoint.post("mcp.resource.read", "/api/mcp/resource/read", {
query: LocationQuery,
payload: Schema.Struct({ server: Schema.String, uri: Schema.String }),
success: Location.response(Schema.NullOr(Mcp.ResourceContent)),
error: McpServerNotFoundError,
})
.annotateMerge(locationQueryOpenApi)
.annotateMerge(
OpenApi.annotations({
identifier: "v2.mcp.resource.read",
summary: "Read MCP resource",
description: "Read the current content of one resource from a connected MCP server.",
}),
),
)
.annotateMerge(OpenApi.annotations({ title: "mcp", description: "MCP server and resource routes." }))
+1
View File
@@ -4,5 +4,6 @@ import { isOpenCodeEvent } from "../src/groups/event.js"
test("classifies public events by type", () => {
expect(isOpenCodeEvent({ type: "server.connected" })).toBe(true)
expect(isOpenCodeEvent({ type: "mcp.status.changed" })).toBe(true)
expect(isOpenCodeEvent({ type: "mcp.resources.changed" })).toBe(true)
expect(isOpenCodeEvent({ type: "mcp.tools.changed" })).toBe(false)
})
+1
View File
@@ -80,6 +80,7 @@ export const ServerDefinitions = Event.inventory(
...InstallationEvent.Definitions,
...VcsEvent.Definitions,
McpEvent.StatusChanged,
McpEvent.ResourcesChanged,
// Shared transitional: V1 contracts the current TUI still consumes during
// the migration (permission.asked/replied, question.asked, session.error).
// Remove when the TUI moves to the current permission/question surfaces.
+1
View File
@@ -9,6 +9,7 @@ export { Form } from "./form.js"
export { Integration } from "./integration.js"
export { LLM } from "./llm.js"
export { Location } from "./location.js"
export { Mcp } from "./mcp.js"
export { Model } from "./model.js"
export { Permission } from "./permission.js"
export { PermissionSaved } from "./permission-saved.js"
+8 -1
View File
@@ -10,6 +10,13 @@ export const ToolsChanged = Event.ephemeral({
},
})
export const ResourcesChanged = Event.ephemeral({
type: "mcp.resources.changed",
schema: {
server: Schema.String,
},
})
export const BrowserOpenFailed = Event.ephemeral({
type: "mcp.browser.open.failed",
schema: {
@@ -27,4 +34,4 @@ export const StatusChanged = Event.ephemeral({
},
})
export const Definitions = Event.inventory(ToolsChanged, StatusChanged)
export const Definitions = Event.inventory(ToolsChanged, ResourcesChanged, StatusChanged)
+50 -8
View File
@@ -25,14 +25,9 @@ const NeedsClientRegistration = Schema.Struct({
}).annotate({ identifier: "Mcp.Status.NeedsClientRegistration" })
export type Status = typeof Status.Type
export const Status = Schema.Union([
Connected,
Pending,
Disabled,
Failed,
NeedsAuth,
NeedsClientRegistration,
]).pipe(Schema.toTaggedUnion("status"))
export const Status = Schema.Union([Connected, Pending, Disabled, Failed, NeedsAuth, NeedsClientRegistration]).pipe(
Schema.toTaggedUnion("status"),
)
export interface Server extends Schema.Schema.Type<typeof Server> {}
export const Server = Schema.Struct({
@@ -42,3 +37,50 @@ export const Server = Schema.Struct({
// without matching by name, which could collide with provider or plugin integrations.
integrationID: optional(IntegrationID),
}).annotate({ identifier: "Mcp.Server" })
export interface Resource extends Schema.Schema.Type<typeof Resource> {}
export const Resource = Schema.Struct({
server: Schema.String,
name: Schema.String,
uri: Schema.String,
description: optional(Schema.String),
mimeType: optional(Schema.String),
}).annotate({ identifier: "Mcp.Resource" })
export interface ResourceTemplate extends Schema.Schema.Type<typeof ResourceTemplate> {}
export const ResourceTemplate = Schema.Struct({
server: Schema.String,
name: Schema.String,
uriTemplate: Schema.String,
description: optional(Schema.String),
mimeType: optional(Schema.String),
}).annotate({ identifier: "Mcp.ResourceTemplate" })
export interface ResourceCatalog extends Schema.Schema.Type<typeof ResourceCatalog> {}
export const ResourceCatalog = Schema.Struct({
resources: Schema.Array(Resource),
templates: Schema.Array(ResourceTemplate),
}).annotate({ identifier: "Mcp.ResourceCatalog" })
export const ResourceContentPart = Schema.Union([
Schema.Struct({
type: Schema.Literal("text"),
uri: Schema.String,
text: Schema.String,
mimeType: optional(Schema.String),
}),
Schema.Struct({
type: Schema.Literal("blob"),
uri: Schema.String,
blob: Schema.String,
mimeType: optional(Schema.String),
}),
]).pipe(Schema.toTaggedUnion("type"), Schema.annotate({ identifier: "Mcp.ResourceContentPart" }))
export type ResourceContentPart = typeof ResourceContentPart.Type
export interface ResourceContent extends Schema.Schema.Type<typeof ResourceContent> {}
export const ResourceContent = Schema.Struct({
server: Schema.String,
uri: Schema.String,
contents: Schema.Array(ResourceContentPart),
}).annotate({ identifier: "Mcp.ResourceContent" })
@@ -2,6 +2,7 @@ import { describe, expect, test } from "bun:test"
import { DateTime, Schema } from "effect"
import { Agent } from "../src/agent.js"
import { FileSystem } from "../src/filesystem.js"
import { Mcp } from "../src/mcp.js"
import { Model } from "../src/model.js"
import { Project } from "../src/project.js"
import { Provider } from "../src/provider.js"
@@ -51,6 +52,11 @@ describe("contract hygiene", () => {
const identifiers = [
Agent.Color,
FileSystem.Submatch,
Mcp.Resource,
Mcp.ResourceTemplate,
Mcp.ResourceCatalog,
Mcp.ResourceContentPart,
Mcp.ResourceContent,
Model.Ref,
Model.Capabilities,
Model.Cost,
+2 -1
View File
@@ -53,6 +53,7 @@ describe("public event manifest", () => {
expect(EventManifest.Server.get("mcp.status.changed")).toBe(McpEvent.StatusChanged)
expect(EventManifest.Server.get("session.deleted")).toBe(SessionEvent.Deleted)
expect(EventManifest.Server.has("mcp.tools.changed")).toBe(false)
expect(EventManifest.Server.get("mcp.resources.changed")).toBe(McpEvent.ResourcesChanged)
expect(Agent.Event.Updated.durable).toBeUndefined()
expect(EventManifest.Durable.has("agent.updated")).toBe(false)
})
@@ -76,7 +77,7 @@ describe("public event manifest", () => {
expect(Form.Event.Definitions).toEqual([Form.Event.Created, Form.Event.Replied, Form.Event.Cancelled])
expect(Reference.Event.Definitions).toEqual([Reference.Event.Updated])
expect(Plugin.Event.Definitions).toEqual([Plugin.Event.Added, Plugin.Event.Updated])
expect(McpEvent.Definitions).toEqual([McpEvent.ToolsChanged, McpEvent.StatusChanged])
expect(McpEvent.Definitions).toEqual([McpEvent.ToolsChanged, McpEvent.ResourcesChanged, McpEvent.StatusChanged])
expect(EventManifest.Latest.has("mcp.browser.open.failed")).toBe(false)
expect(EventManifest.Latest.has("ide.installed")).toBe(false)
expect(IdeEvent.Definitions).toEqual([IdeEvent.Installed])
+37
View File
@@ -0,0 +1,37 @@
import { describe, expect, test } from "bun:test"
import { Schema } from "effect"
import { Mcp } from "../src/mcp.js"
describe("Mcp resources", () => {
test("decodes resource catalogs and omits absent metadata", () => {
const value = Schema.decodeUnknownSync(Mcp.ResourceCatalog)({
resources: [{ server: "docs", name: "Readme", uri: "docs://readme" }],
templates: [{ server: "docs", name: "File", uriTemplate: "docs://{path}" }],
})
expect(Schema.encodeSync(Mcp.ResourceCatalog)(value)).toEqual({
resources: [{ server: "docs", name: "Readme", uri: "docs://readme" }],
templates: [{ server: "docs", name: "File", uriTemplate: "docs://{path}" }],
})
})
test("preserves text and base64 blob contents", () => {
expect(
Schema.decodeUnknownSync(Mcp.ResourceContent)({
server: "docs",
uri: "docs://readme",
contents: [
{ type: "text", uri: "docs://readme", text: "hello", mimeType: "text/plain" },
{ type: "blob", uri: "docs://logo", blob: "aGVsbG8=", mimeType: "image/png" },
],
}),
).toEqual({
server: "docs",
uri: "docs://readme",
contents: [
{ type: "text", uri: "docs://readme", text: "hello", mimeType: "text/plain" },
{ type: "blob", uri: "docs://logo", blob: "aGVsbG8=", mimeType: "image/png" },
],
})
})
})
+1
View File
@@ -9,6 +9,7 @@ export { Credential } from "@opencode-ai/schema/credential"
export { FileSystem } from "@opencode-ai/schema/filesystem"
export { Integration } from "@opencode-ai/schema/integration"
export { Location } from "@opencode-ai/schema/location"
export { Mcp } from "@opencode-ai/schema/mcp"
export { Model } from "@opencode-ai/schema/model"
export { Permission } from "@opencode-ai/schema/permission"
export { PermissionSaved } from "@opencode-ai/schema/permission-saved"
@@ -1,6 +1,7 @@
import { expect, test } from "bun:test"
import { Agent } from "@opencode-ai/schema/agent"
import { Model } from "@opencode-ai/schema/model"
import { Mcp } from "@opencode-ai/schema/mcp"
import { Session } from "@opencode-ai/schema/session"
const SDK = await import("../src/index")
@@ -8,6 +9,7 @@ const SDK = await import("../src/index")
test("re-exports canonical contracts directly from Schema", () => {
expect(SDK.Agent).toBe(Agent)
expect(SDK.Model).toBe(Model)
expect(SDK.Mcp).toBe(Mcp)
expect(SDK.Session).toBe(Session)
expect(Object.keys(SDK).sort()).toEqual([
"AbsolutePath",
@@ -18,6 +20,7 @@ test("re-exports canonical contracts directly from Schema", () => {
"FileSystem",
"Integration",
"Location",
"Mcp",
"Model",
"OpenCode",
"Permission",
+77
View File
@@ -310,6 +310,10 @@ import type {
V2LocationGetResponses,
V2McpListErrors,
V2McpListResponses,
V2McpResourceCatalogErrors,
V2McpResourceCatalogResponses,
V2McpResourceReadErrors,
V2McpResourceReadResponses,
V2ModelDefaultErrors,
V2ModelDefaultResponses,
V2ModelListErrors,
@@ -6969,6 +6973,74 @@ export class Integration extends HeyApiClient {
}
}
export class Resource2 extends HeyApiClient {
/**
* List MCP resources
*
* Retrieve resources and resource templates from connected MCP servers.
*/
public catalog<ThrowOnError extends boolean = false>(
parameters?: {
location?: {
directory?: string | null
workspace?: string | null
} | null
},
options?: Options<never, ThrowOnError>,
) {
const params = buildClientParams([parameters], [{ args: [{ in: "query", key: "location" }] }])
return (options?.client ?? this.client).get<
V2McpResourceCatalogResponses,
V2McpResourceCatalogErrors,
ThrowOnError
>({
url: "/api/mcp/resource",
...options,
...params,
})
}
/**
* Read MCP resource
*
* Read the current content of one resource from a connected MCP server.
*/
public read<ThrowOnError extends boolean = false>(
parameters?: {
location?: {
directory?: string | null
workspace?: string | null
} | null
server?: string
uri?: string
},
options?: Options<never, ThrowOnError>,
) {
const params = buildClientParams(
[parameters],
[
{
args: [
{ in: "query", key: "location" },
{ in: "body", key: "server" },
{ in: "body", key: "uri" },
],
},
],
)
return (options?.client ?? this.client).post<V2McpResourceReadResponses, V2McpResourceReadErrors, ThrowOnError>({
url: "/api/mcp/resource/read",
...options,
...params,
headers: {
"Content-Type": "application/json",
...options?.headers,
...params.headers,
},
})
}
}
export class Mcp2 extends HeyApiClient {
/**
* List MCP servers
@@ -6991,6 +7063,11 @@ export class Mcp2 extends HeyApiClient {
...params,
})
}
private _resource?: Resource2
get resource(): Resource2 {
return (this._resource ??= new Resource2({ client: this.client }))
}
}
export class Credential extends HeyApiClient {
+191
View File
@@ -95,6 +95,7 @@ export type Event =
| EventTuiToastShow2
| EventTuiSessionSelect2
| EventMcpToolsChanged
| EventMcpResourcesChanged
| EventMcpStatusChanged
| EventCommandExecuted
| EventFileEdited
@@ -1603,6 +1604,13 @@ export type GlobalEvent = {
server: string
}
}
| {
id: string
type: "mcp.resources.changed"
properties: {
server: string
}
}
| {
id: string
type: "mcp.status.changed"
@@ -2991,6 +2999,20 @@ export type ProviderNotFoundError = {
message: string
}
export type McpResource2 = {
server: string
name: string
uri: string
description?: string
mimeType?: string
}
export type McpServerNotFoundError1 = {
_tag: "McpServerNotFoundError"
server: string
message: string
}
export type FormNotFoundError = {
_tag: "FormNotFoundError"
id: string
@@ -3153,6 +3175,7 @@ export type V2Event =
| TuiToastShow
| TuiSessionSelect
| McpToolsChanged
| McpResourcesChanged
| McpStatusChanged
| CommandExecuted
| FileEdited
@@ -5702,6 +5725,39 @@ export type McpServer = {
integrationID?: string
}
export type McpResourceTemplate = {
server: string
name: string
uriTemplate: string
description?: string
mimeType?: string
}
export type McpResourceCatalog = {
resources: Array<McpResource2>
templates: Array<McpResourceTemplate>
}
export type McpResourceContentPart =
| {
type: "text"
uri: string
text: string
mimeType?: string
}
| {
type: "blob"
uri: string
blob: string
mimeType?: string
}
export type McpResourceContent = {
server: string
uri: string
contents: Array<McpResourceContentPart>
}
export type ProjectCurrent = {
id: string
directory: string
@@ -6608,6 +6664,19 @@ export type McpToolsChanged = {
}
}
export type McpResourcesChanged = {
id: string
created: number
metadata?: {
[key: string]: unknown
}
type: "mcp.resources.changed"
location?: LocationRef
data: {
server: string
}
}
export type McpStatusChanged = {
id: string
created: number
@@ -7760,6 +7829,14 @@ export type EventMcpToolsChanged = {
}
}
export type EventMcpResourcesChanged = {
id: string
type: "mcp.resources.changed"
properties: {
server: string
}
}
export type EventMcpStatusChanged = {
id: string
type: "mcp.status.changed"
@@ -8034,6 +8111,12 @@ export type SessionMessagesResponseV2 = {
}
}
export type McpServerNotFoundErrorV2 = {
_tag: "McpServerNotFoundError"
server: string
message: string
}
export type SessionV2 = {
id: string
slug: string
@@ -8597,6 +8680,7 @@ export type V2EventV2 =
| InstallationUpdateAvailableV2
| VcsBranchUpdatedV2
| McpStatusChangedV2
| McpResourcesChangedV2
| PermissionAskedV2
| PermissionRepliedV2
| QuestionAskedV2
@@ -9686,6 +9770,19 @@ export type EventLogSyncedV2 = {
seq?: number
}
export type McpResourceV2 = {
server: string
name: string
uri: string
description?: string
mimeType?: string
}
export type McpResourceCatalogV2 = {
resources: Array<McpResourceV2>
templates: Array<McpResourceTemplate>
}
export type ProjectTimeV2 = {
created: number
updated: number
@@ -10615,6 +10712,19 @@ export type McpStatusChangedV2 = {
}
}
export type McpResourcesChangedV2 = {
id: string
created: number
metadata?: {
[key: string]: unknown
}
type: "mcp.resources.changed"
location?: LocationRefV2
data: {
server: string
}
}
export type PermissionAskedV2 = {
id: string
created: number
@@ -16685,6 +16795,87 @@ export type V2McpListResponses = {
export type V2McpListResponse = V2McpListResponses[keyof V2McpListResponses]
export type V2McpResourceCatalogData = {
body?: never
path?: never
query?: {
location?: {
directory?: string | null
workspace?: string | null
} | null
}
url: "/api/mcp/resource"
}
export type V2McpResourceCatalogErrors = {
/**
* InvalidRequestError
*/
400: InvalidRequestErrorV2
/**
* UnauthorizedError
*/
401: UnauthorizedError
}
export type V2McpResourceCatalogError = V2McpResourceCatalogErrors[keyof V2McpResourceCatalogErrors]
export type V2McpResourceCatalogResponses = {
/**
* Success
*/
200: {
location: LocationInfoV2
data: McpResourceCatalogV2
}
}
export type V2McpResourceCatalogResponse = V2McpResourceCatalogResponses[keyof V2McpResourceCatalogResponses]
export type V2McpResourceReadData = {
body: {
server: string
uri: string
}
path?: never
query?: {
location?: {
directory?: string | null
workspace?: string | null
} | null
}
url: "/api/mcp/resource/read"
}
export type V2McpResourceReadErrors = {
/**
* InvalidRequestError
*/
400: InvalidRequestErrorV2
/**
* UnauthorizedError
*/
401: UnauthorizedError
/**
* McpServerNotFoundError
*/
404: McpServerNotFoundErrorV2
}
export type V2McpResourceReadError = V2McpResourceReadErrors[keyof V2McpResourceReadErrors]
export type V2McpResourceReadResponses = {
/**
* Success
*/
200: {
location: LocationInfoV2
data: McpResourceContent | null
}
}
export type V2McpResourceReadResponse = V2McpResourceReadResponses[keyof V2McpResourceReadResponses]
export type V2CredentialRemoveData = {
body?: never
path: {
+34 -13
View File
@@ -2,24 +2,45 @@ import { MCP } from "@opencode-ai/core/mcp/index"
import { Effect } from "effect"
import { HttpApiBuilder } from "effect/unstable/httpapi"
import { Api } from "../api"
import { McpServerNotFoundError } from "@opencode-ai/protocol/errors"
import { response } from "../location"
export const McpHandler = HttpApiBuilder.group(Api, "server.mcp", (handlers) =>
Effect.gen(function* () {
return handlers.handle(
"mcp.list",
Effect.fn(function* () {
const service = yield* MCP.Service
return yield* response(
service
.servers()
.pipe(
Effect.map((servers) =>
servers.map((info) => ({ name: info.name, status: info.status, integrationID: info.integrationID })),
return handlers
.handle(
"mcp.list",
Effect.fn(function* () {
const service = yield* MCP.Service
return yield* response(
service
.servers()
.pipe(
Effect.map((servers) =>
servers.map((info) => ({ name: info.name, status: info.status, integrationID: info.integrationID })),
),
),
)
}),
)
.handle(
"mcp.resource.catalog",
Effect.fn(function* () {
const service = yield* MCP.Service
return yield* response(service.resourceCatalog())
}),
)
.handle(
"mcp.resource.read",
Effect.fn(function* (ctx) {
const service = yield* MCP.Service
return yield* response(
service.readResource({ server: ctx.payload.server, uri: ctx.payload.uri }).pipe(
Effect.map((result) => result ?? null),
Effect.mapError((error) => new McpServerNotFoundError({ server: error.server, message: error.message })),
),
)
}),
)
)
}),
)
}),
)
+6 -9
View File
@@ -10,7 +10,7 @@ import { HealthGroup } from "@opencode-ai/protocol/groups/health"
import { Context, Effect, Layer, Option } from "effect"
import { HttpClient, HttpClientRequest, HttpRouter, HttpServer } from "effect/unstable/http"
import { HttpApi, HttpApiClient } from "effect/unstable/httpapi"
import { createServer, type Server } from "node:http"
import { createServer } from "node:http"
import { ServerAuth } from "./auth"
import { createRoutes } from "./routes"
@@ -18,7 +18,6 @@ export type Options = {
readonly hostname: string
readonly port: Option.Option<number>
readonly password: string
readonly replacements?: (server: Server) => LayerNode.Replacements
}
const ReadinessApi = HttpApi.make("readiness").add(HealthGroup)
@@ -43,21 +42,19 @@ export const start = Effect.fn("ServerProcess.start")(function* (options: Option
})
function listen(options: Options) {
if (Option.isSome(options.port)) return bind(options, options.port.value)
if (Option.isSome(options.port)) return bind(options.hostname, options.port.value, options.password)
const next = (port: number): ReturnType<typeof bind> =>
bind(options, port).pipe(
bind(options.hostname, port, options.password).pipe(
Effect.catch((error) => (port === 65_535 ? Effect.fail(error) : next(port + 1))),
)
return next(4096)
}
function bind(options: Options, port: number) {
function bind(hostname: string, port: number, password: string) {
const server = createServer()
return Layer.build(
HttpRouter.serve(createRoutes(options.password, options.replacements?.(server)), {
disableListenLog: true,
}).pipe(
Layer.provideMerge(NodeHttpServer.layer(() => server, { port, host: options.hostname })),
HttpRouter.serve(createRoutes(password), { disableListenLog: true }).pipe(
Layer.provideMerge(NodeHttpServer.layer(() => server, { port, host: hostname })),
Layer.provide(AppNodeBuilder.build(LayerNode.group([Credential.node, PermissionSaved.node, Project.node]))),
),
).pipe(
+3 -11
View File
@@ -47,28 +47,21 @@ const applicationServices = LayerNode.group([
LocationServiceMap.node,
])
export function createRoutes(password?: string, replacements: LayerNode.Replacements = []) {
export function createRoutes(password?: string) {
return makeRoutes(
password
? ServerAuth.Config.configLayer({ username: "opencode", password: Option.some(password) })
: ServerAuth.Config.layer,
undefined,
replacements,
)
}
export function createEmbeddedRoutes(sdkPlugins?: SdkPlugins.Store, replacements: LayerNode.Replacements = []) {
return makeRoutes(
ServerAuth.Config.configLayer({ username: "opencode", password: Option.none() }),
sdkPlugins,
replacements,
)
export function createEmbeddedRoutes(sdkPlugins?: SdkPlugins.Store) {
return makeRoutes(ServerAuth.Config.configLayer({ username: "opencode", password: Option.none() }), sdkPlugins)
}
function makeRoutes<AuthError, AuthServices>(
auth: Layer.Layer<ServerAuth.Config, AuthError, AuthServices>,
sdkPlugins?: SdkPlugins.Store,
hostReplacements: LayerNode.Replacements = [],
) {
const pluginRuntimeCell = PluginRuntime.makeCell()
const replacements: LayerNode.Replacements = [
@@ -76,7 +69,6 @@ function makeRoutes<AuthError, AuthServices>(
[PluginRuntime.node, PluginRuntime.layerWithCell(pluginRuntimeCell)],
[PluginRuntime.providerNode, PluginRuntime.providerNodeWithCell(pluginRuntimeCell)],
...(sdkPlugins ? [[SdkPlugins.node, SdkPlugins.layerWithStore(sdkPlugins)] as const] : []),
...hostReplacements,
]
const serviceLayer = simulateEnabled()
? Layer.unwrap(
@@ -207,7 +207,13 @@ export function Autocomplete(props: {
props.setPrompt((draft) => {
if (part.type === "file") {
const files = (draft.files ??= [])
const existingIndex = files.findIndex((file) => file.uri === part.value.uri)
const existingIndex = files.findIndex(
(file) =>
file.uri === part.value.uri &&
file.mcp?.server === part.value.mcp?.server &&
file.mcp?.location.directory === part.value.mcp?.location.directory &&
file.mcp?.location.workspaceID === part.value.mcp?.location.workspaceID,
)
if (existingIndex !== -1) {
const existing = files[existingIndex]
if (existing?.mention) {
@@ -215,6 +221,7 @@ export function Autocomplete(props: {
existing.mention.end = extmarkEnd
existing.mention.text = virtualText
}
props.setExtmark({ type: "file", index: existingIndex }, extmarkId)
return
}
if (part.value.mention) {
@@ -363,7 +370,7 @@ export function Autocomplete(props: {
const options: AutocompleteOption[] = []
const width = props.anchor().width - 4
for (const res of Object.values(sync.data.mcp_resource)) {
for (const res of data.location.mcp.resource.catalog(location())?.resources ?? []) {
options.push({
display: Locale.truncateMiddle(res.name, width),
// Match the name only; matching the URI caused unrelated fuzzy hits.
@@ -377,6 +384,7 @@ export function Autocomplete(props: {
name: res.name,
description: res.description,
mention: { start: 0, end: 0, text: "" },
mcp: { server: res.server, uri: res.uri, location: location() ?? data.location.default() },
},
})
},
+47 -32
View File
@@ -31,6 +31,7 @@ import { useExit } from "../../context/exit"
import { promptOffsetWidth } from "../../prompt/display"
import { createStore, produce, unwrap } from "solid-js/store"
import { emptyPrompt, usePromptHistory, type PromptInfo, type PromptPartRef } from "../../prompt/history"
import { materializeMcpResources } from "./mcp-resource"
import { computePromptTraits } from "../../prompt/traits"
import { expandPastedTextPlaceholders, expandTrackedPastedText } from "../../prompt/part"
import { usePromptStash } from "../../prompt/stash"
@@ -164,9 +165,9 @@ export function Prompt(props: PromptProps) {
const status = createMemo(() => data.session.status(props.sessionID ?? ""))
const activeSubagents = createMemo(() => {
if (!props.sessionID) return 0
return data.session.family(props.sessionID).filter(
(id) => id !== props.sessionID && data.session.status(id) === "running",
).length
return data.session
.family(props.sessionID)
.filter((id) => id !== props.sessionID && data.session.status(id) === "running").length
})
const runningShells = createMemo(
() => data.shell.list(currentLocation()).filter((shell) => shell.metadata.sessionID === props.sessionID).length,
@@ -984,6 +985,36 @@ export function Prompt(props: PromptProps) {
}
const variant = local.model.variant.current()
const inputText = expandTrackedPastedText(
store.prompt.text,
input.extmarks.getAllForTypeId(promptPartTypeId).flatMap((extmark) => {
const ref = store.extmarkToPart.get(extmark.id)
if (ref?.type !== "pasted") return []
const part = store.prompt.pasted[ref.index]
if (!part) return []
return [{ start: extmark.start, end: extmark.end, text: part.text }]
}),
)
const commandName = inputText.split("\n")[0].split(" ")[0].slice(1)
const isCommand =
inputText.startsWith("/") &&
(data.location.command.list(currentLocation()) ?? []).some((command) => command.name === commandName)
const isSkill =
inputText.startsWith("/") &&
(data.location.skill.list(currentLocation()) ?? []).some(
(skill) => skill.slash === true && skill.name === commandName,
)
const files =
store.mode === "normal" && !isSkill
? await materializeMcpResources(store.prompt.files, (resource) =>
data.location.mcp.resource.read(resource),
).catch((error) => {
toast.show({ title: "Failed to read MCP resource", message: errorMessage(error), variant: "error" })
return undefined
})
: []
if (!files) return false
let sessionID = props.sessionID
let session = sessionID ? data.session.get(sessionID) : undefined
let finishMoveProgress = false
@@ -1025,17 +1056,6 @@ export function Prompt(props: PromptProps) {
session = structuredClone(created) as SessionV2Info
}
const inputText = expandTrackedPastedText(
store.prompt.text,
input.extmarks.getAllForTypeId(promptPartTypeId).flatMap((extmark) => {
const ref = store.extmarkToPart.get(extmark.id)
if (ref?.type !== "pasted") return []
const part = store.prompt.pasted[ref.index]
if (!part) return []
return [{ start: extmark.start, end: extmark.end, text: part.text }]
}),
)
// Capture mode before it gets reset
const currentMode = store.mode
const editorSelection = editorContext()
@@ -1063,12 +1083,7 @@ export function Prompt(props: PromptProps) {
command: inputText,
})
setStore("mode", "normal")
} else if (
inputText.startsWith("/") &&
(data.location.command.list(currentLocation()) ?? []).some(
(command) => command.name === inputText.split("\n")[0].split(" ")[0].slice(1),
)
) {
} else if (isCommand) {
move.startSubmit()
// Parse command from first line, preserve multi-line content in arguments
const firstLineEnd = inputText.indexOf("\n")
@@ -1077,25 +1092,25 @@ export function Prompt(props: PromptProps) {
const restOfInput = firstLineEnd === -1 ? "" : inputText.slice(firstLineEnd + 1)
const args = firstLineArgs.join(" ") + (restOfInput ? "\n" + restOfInput : "")
void sdk.api.session
const error = await sdk.api.session
.command({
sessionID,
command: command.slice(1),
arguments: args,
agent: agent.id,
model: { providerID: selectedModel.providerID, id: selectedModel.modelID, variant },
files: store.prompt.files,
files,
agents: store.prompt.agents,
})
.catch((error) => {
toast.show({ title: "Failed to run command", message: errorMessage(error), variant: "error" })
})
} else if (
inputText.startsWith("/") &&
(data.location.skill.list(currentLocation()) ?? []).some(
(skill) => skill.slash === true && skill.name === inputText.split("\n")[0].split(" ")[0].slice(1),
)
) {
.then(
() => undefined,
(error) => error,
)
if (error) {
toast.show({ title: "Failed to run command", message: errorMessage(error), variant: "error" })
return false
}
} else if (isSkill) {
move.startSubmit()
void sdk.api.session.skill({
sessionID,
@@ -1135,7 +1150,7 @@ export function Prompt(props: PromptProps) {
sessionID,
prompt: {
text: [...editorParts.map((part) => part.text), inputText].filter(Boolean).join("\n\n"),
files: store.prompt.files,
files,
agents: store.prompt.agents,
},
})
@@ -0,0 +1,32 @@
import { Buffer } from "node:buffer"
import type { ServerMcpReadResourceOutput, SessionPromptInput } from "@opencode-ai/client/promise"
import type { LocationRef } from "@opencode-ai/sdk/v2"
import type { PromptFile } from "../../prompt/history"
type Files = NonNullable<SessionPromptInput["prompt"]["files"]>
type ResourceContent = NonNullable<ServerMcpReadResourceOutput["data"]>
export async function materializeMcpResources(
files: PromptFile[] | undefined,
read: (input: { server: string; uri: string; location: LocationRef }) => Promise<ResourceContent | null>,
): Promise<Files> {
return (
await Promise.all(
(files ?? []).map(async (file): Promise<Files> => {
if (!file.mcp) return [{ uri: file.uri, name: file.name, description: file.description, mention: file.mention }]
const resource = await read(file.mcp)
if (!resource) throw new Error(`Unable to read MCP resource: ${file.mcp.server}:${file.mcp.uri}`)
if (resource.contents.length === 0)
throw new Error(`MCP resource returned no content: ${file.mcp.server}:${file.mcp.uri}`)
return resource.contents.map((content, index) => ({
uri: `data:${content.mimeType ?? (content.type === "text" ? "text/plain" : "application/octet-stream")};base64,${
content.type === "text" ? Buffer.from(content.text).toString("base64") : content.blob
}`,
name: index === 0 ? file.name : `${file.name ?? "resource"}-${index + 1}`,
description: file.description,
mention: index === 0 ? file.mention : undefined,
}))
}),
)
).flat()
}
+50 -5
View File
@@ -21,6 +21,7 @@ import type {
SkillV2Info,
V2Event,
} from "@opencode-ai/sdk/v2"
import type { ServerMcpResourceCatalogOutput, ServerMcpReadResourceOutput } from "@opencode-ai/client/promise"
import { createStore, produce, reconcile } from "solid-js/store"
import { createSimpleContext } from "./helper"
import { useSDK } from "./sdk"
@@ -37,6 +38,7 @@ type LocationData = {
command?: CommandV2Info[]
integration?: IntegrationInfo[]
mcp?: McpServer[]
mcpResource?: ServerMcpResourceCatalogOutput["data"]
model?: ModelV2Info[]
provider?: ProviderV2Info[]
reference?: ReferenceInfo[]
@@ -113,6 +115,7 @@ export const { use: useData, provider: DataProvider } = createSimpleContext({
let connectionGeneration = 0
let statusChanges: Set<string> | undefined
let bootstrapping: Promise<void> | undefined
const pendingMcpRefresh = new Map<string, { location: LocationRef; status: boolean }>()
function setSessionStatus(sessionID: string, status: DataSessionStatus) {
statusChanges?.add(sessionID)
@@ -754,8 +757,24 @@ export const { use: useData, provider: DataProvider } = createSimpleContext({
// Authenticating an MCP integration reconnects its server, which emits mcp.status.changed,
// so the mcp list refreshes here rather than off integration.updated.
case "mcp.status.changed":
if (bootstrapping) break
void result.location.mcp.refresh(event.location)
if (bootstrapping) {
const location = event.location ?? defaultLocation()
pendingMcpRefresh.set(locationKey(location), { location, status: true })
break
}
void Promise.all([
result.location.mcp.refresh(event.location),
result.location.mcp.resource.refresh(event.location),
])
break
case "mcp.resources.changed":
if (bootstrapping) {
const location = event.location ?? defaultLocation()
const pending = pendingMcpRefresh.get(locationKey(location))
pendingMcpRefresh.set(locationKey(location), { location, status: pending?.status ?? false })
break
}
void result.location.mcp.resource.refresh(event.location)
break
}
}
@@ -938,9 +957,27 @@ export const { use: useData, provider: DataProvider } = createSimpleContext({
return store.location[locationKey(location ?? defaultLocation())]?.mcp
},
async refresh(ref?: LocationRef) {
const result = await sdk.client.v2.mcp.list({ location: locationQuery(ref) }, { throwOnError: true })
const key = locationKey(result.data.location)
setStore("location", key, { ...store.location[key], mcp: result.data.data })
const result = await sdk.api["server.mcp"].list({ location: locationQuery(ref) })
const key = locationKey(result.location)
setStore("location", key, { ...store.location[key], mcp: mutable(result.data) })
},
resource: {
catalog(location?: LocationRef) {
return store.location[locationKey(location ?? defaultLocation())]?.mcpResource
},
async refresh(ref?: LocationRef) {
const result = await sdk.api["server.mcp"].resourceCatalog({ location: locationQuery(ref) })
const key = locationKey(result.location)
setStore("location", key, { ...store.location[key], mcpResource: mutable(result.data) })
},
async read(input: { server: string; uri: string; location?: LocationRef }) {
const result = await sdk.api["server.mcp"].readResource({
server: input.server,
uri: input.uri,
location: locationQuery(input.location),
})
return result.data as ServerMcpReadResourceOutput["data"]
},
},
},
model: {
@@ -1010,6 +1047,7 @@ export const { use: useData, provider: DataProvider } = createSimpleContext({
result.location.agent.refresh(),
result.location.integration.refresh(),
result.location.mcp.refresh(),
result.location.mcp.resource.refresh(),
result.location.model.refresh(),
result.location.provider.refresh(),
result.location.reference.refresh(),
@@ -1023,6 +1061,13 @@ export const { use: useData, provider: DataProvider } = createSimpleContext({
})
.finally(() => {
bootstrapping = undefined
for (const pending of pendingMcpRefresh.values()) {
void Promise.all([
...(pending.status ? [result.location.mcp.refresh(pending.location)] : []),
result.location.mcp.resource.refresh(pending.location),
])
}
pendingMcpRefresh.clear()
})
return bootstrapping
}
+9 -1
View File
@@ -2,6 +2,7 @@ import path from "path"
import { onMount } from "solid-js"
import { createStore, produce, unwrap } from "solid-js/store"
import type { SessionPromptInput } from "@opencode-ai/client/promise"
import type { LocationRef } from "@opencode-ai/sdk/v2"
import type { Types } from "effect"
import { createSimpleContext } from "../context/helper"
import { useTuiPaths } from "../context/runtime"
@@ -16,7 +17,14 @@ export type PastedText = {
}
}
export type PromptInfo = Types.DeepMutable<SessionPromptInput["prompt"]> & {
type Prompt = Types.DeepMutable<SessionPromptInput["prompt"]>
export type PromptFile = NonNullable<Prompt["files"]>[number] & {
mcp?: { server: string; uri: string; location: LocationRef }
}
export type PromptInfo = Omit<Prompt, "files"> & {
files?: PromptFile[]
pasted: PastedText[]
mode?: "normal" | "shell"
}
+57
View File
@@ -108,6 +108,63 @@ test("refreshes resources into reactive getters", async () => {
}
})
test("refreshes MCP resource catalogs after MCP events", async () => {
const events = createEventStream()
let resources = [{ server: "docs", name: "Readme", uri: "docs://readme" }]
let requests = 0
const calls = createFetch((url) => {
if (url.pathname !== "/api/mcp/resource") return
requests++
return json({
location: { directory, project: { id: "proj_test", directory } },
data: { resources, templates: [] },
})
}, events)
let data!: ReturnType<typeof useData>
function Probe() {
data = useData()
return <text>{data.location.mcp.resource.catalog()?.resources[0]?.name ?? "missing"}</text>
}
const app = await testRender(() => (
<TestTuiContexts>
<SDKProvider client={createClient(calls.fetch)} api={createApi(calls.fetch)}>
<ProjectProvider>
<DataProvider>
<Probe />
</DataProvider>
</ProjectProvider>
</SDKProvider>
</TestTuiContexts>
))
try {
await wait(() => requests === 1)
expect(data.location.mcp.resource.catalog()?.resources[0]?.uri).toBe("docs://readme")
resources = [{ server: "docs", name: "Guide", uri: "docs://guide" }]
emitEvent(events, {
id: "evt_mcp_resources",
created: 1,
type: "mcp.resources.changed",
data: { server: "docs" },
})
await wait(() => requests === 2 && data.location.mcp.resource.catalog()?.resources[0]?.name === "Guide")
resources = []
emitEvent(events, {
id: "evt_mcp_status",
created: 2,
type: "mcp.status.changed",
data: { server: "docs" },
})
await wait(() => requests === 3 && data.location.mcp.resource.catalog()?.resources.length === 0)
} finally {
app.renderer.destroy()
}
})
test("restores running manual compaction before applying live deltas", async () => {
const events = createEventStream()
const calls = createFetch((url) => {
@@ -0,0 +1,60 @@
import { describe, expect, test } from "bun:test"
import { materializeMcpResources } from "../../../src/component/prompt/mcp-resource"
describe("MCP resource prompt attachments", () => {
test("materializes text and blob content while preserving one mention", async () => {
const calls: Array<{ server: string; uri: string; location: { directory: string } }> = []
const files = await materializeMcpResources(
[
{
uri: "docs://readme",
name: "Readme",
description: "Project docs",
mention: { start: 0, end: 7, text: "@Readme" },
mcp: { server: "docs", uri: "docs://readme", location: { directory: "/tmp/project" } },
},
],
async (input) => {
calls.push(input)
return {
server: input.server,
uri: input.uri,
contents: [
{ type: "text", uri: input.uri, text: "hello", mimeType: "text/plain" },
{ type: "blob", uri: "docs://logo", blob: "aGVsbG8=", mimeType: "image/png" },
],
}
},
)
expect(calls).toEqual([{ server: "docs", uri: "docs://readme", location: { directory: "/tmp/project" } }])
expect(files).toEqual([
{
uri: "data:text/plain;base64,aGVsbG8=",
name: "Readme",
description: "Project docs",
mention: { start: 0, end: 7, text: "@Readme" },
},
{
uri: "data:image/png;base64,aGVsbG8=",
name: "Readme-2",
description: "Project docs",
mention: undefined,
},
])
})
test("fails when a resource is unavailable", async () => {
await expect(
materializeMcpResources(
[
{
uri: "docs://missing",
mcp: { server: "docs", uri: "docs://missing", location: { directory: "/tmp/project" } },
},
],
async () => null,
),
).rejects.toThrow("Unable to read MCP resource: docs:docs://missing")
})
})
+5
View File
@@ -99,6 +99,11 @@ export function createFetch(override?: FetchHandler, events?: ReturnType<typeof
return json({ location: { directory, project: { id: "proj_test", directory: worktree } }, data: [] })
if (url.pathname === "/api/mcp")
return json({ location: { directory, project: { id: "proj_test", directory: worktree } }, data: [] })
if (url.pathname === "/api/mcp/resource")
return json({
location: { directory, project: { id: "proj_test", directory: worktree } },
data: { resources: [], templates: [] },
})
if (url.pathname === "/api/session") return json({ data: [], cursor: {} })
if (url.pathname === "/api/session/active") return json({ data: {} })
if (/^\/api\/session\/[^/]+\/form$/.test(url.pathname)) return json({ data: [] })