Skip to content

Commit 011c237

Browse files
authored
feat(httpapi): bridge mcp status endpoint (#24100)
1 parent a8c8d2d commit 011c237

7 files changed

Lines changed: 138 additions & 50 deletions

File tree

packages/opencode/specs/effect/http-api.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -415,8 +415,9 @@ Current instance route inventory:
415415
- `file` - `bridged` (partial)
416416
bridged endpoints: `GET /file`, `GET /file/content`, `GET /file/status`
417417
defer search endpoints first
418-
- `mcp` - `later`
419-
has JSON-only endpoints, but interactive OAuth/auth flows make it a worse early fit
418+
- `mcp` - `bridged` (partial)
419+
bridged endpoints: `GET /mcp`
420+
defer interactive OAuth/auth flows first
420421
- `session` - `defer`
421422
large, stateful, mixes CRUD with prompt/shell/command/share/revert flows and a streaming route
422423
- `event` - `defer`
@@ -451,6 +452,7 @@ Recommended near-term sequence:
451452
- [x] port `GET /config` full read endpoint
452453
- [x] port `workspace` read endpoints
453454
- [x] port `file` JSON read endpoints
455+
- [x] port `mcp` status read endpoint
454456
- [ ] decide when to remove the flag and make Effect routes the default
455457

456458
## Rule of thumb

packages/opencode/src/mcp/index.ts

Lines changed: 29 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ import { EffectBridge } from "@/effect"
3030
import { InstanceState } from "@/effect"
3131
import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process"
3232
import * as CrossSpawnSpawner from "@/effect/cross-spawn-spawner"
33+
import { zod as effectZod } from "@/util/effect-zod"
34+
import { withStatics } from "@/util/schema"
3335

3436
const log = Log.create({ service: "mcp" })
3537
const DEFAULT_TIMEOUT = 30_000
@@ -69,50 +71,33 @@ export const Failed = NamedError.create(
6971

7072
type MCPClient = Client
7173

72-
export const Status = z
73-
.discriminatedUnion("status", [
74-
z
75-
.object({
76-
status: z.literal("connected"),
77-
})
78-
.meta({
79-
ref: "MCPStatusConnected",
80-
}),
81-
z
82-
.object({
83-
status: z.literal("disabled"),
84-
})
85-
.meta({
86-
ref: "MCPStatusDisabled",
87-
}),
88-
z
89-
.object({
90-
status: z.literal("failed"),
91-
error: z.string(),
92-
})
93-
.meta({
94-
ref: "MCPStatusFailed",
95-
}),
96-
z
97-
.object({
98-
status: z.literal("needs_auth"),
99-
})
100-
.meta({
101-
ref: "MCPStatusNeedsAuth",
102-
}),
103-
z
104-
.object({
105-
status: z.literal("needs_client_registration"),
106-
error: z.string(),
107-
})
108-
.meta({
109-
ref: "MCPStatusNeedsClientRegistration",
110-
}),
111-
])
112-
.meta({
113-
ref: "MCPStatus",
114-
})
115-
export type Status = z.infer<typeof Status>
74+
const StatusConnected = Schema.Struct({ status: Schema.Literal("connected") }).annotate({
75+
identifier: "MCPStatusConnected",
76+
})
77+
const StatusDisabled = Schema.Struct({ status: Schema.Literal("disabled") }).annotate({
78+
identifier: "MCPStatusDisabled",
79+
})
80+
const StatusFailed = Schema.Struct({ status: Schema.Literal("failed"), error: Schema.String }).annotate({
81+
identifier: "MCPStatusFailed",
82+
})
83+
const StatusNeedsAuth = Schema.Struct({ status: Schema.Literal("needs_auth") }).annotate({
84+
identifier: "MCPStatusNeedsAuth",
85+
})
86+
const StatusNeedsClientRegistration = Schema.Struct({
87+
status: Schema.Literal("needs_client_registration"),
88+
error: Schema.String,
89+
}).annotate({ identifier: "MCPStatusNeedsClientRegistration" })
90+
91+
export const Status = Schema.Union([
92+
StatusConnected,
93+
StatusDisabled,
94+
StatusFailed,
95+
StatusNeedsAuth,
96+
StatusNeedsClientRegistration,
97+
])
98+
.annotate({ identifier: "MCPStatus", discriminator: "status" })
99+
.pipe(withStatics((s) => ({ zod: effectZod(s) })))
100+
export type Status = Schema.Schema.Type<typeof Status>
116101

117102
// Store transports for OAuth servers to allow finishing auth
118103
type TransportWithAuth = StreamableHTTPClientTransport | SSEClientTransport
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { MCP } from "@/mcp"
2+
import { Effect, Layer, Schema } from "effect"
3+
import { HttpApi, HttpApiBuilder, HttpApiEndpoint, HttpApiGroup, OpenApi } from "effect/unstable/httpapi"
4+
5+
export const McpPaths = {
6+
status: "/mcp",
7+
} as const
8+
9+
export const McpApi = HttpApi.make("mcp")
10+
.add(
11+
HttpApiGroup.make("mcp")
12+
.add(
13+
HttpApiEndpoint.get("status", McpPaths.status, {
14+
success: Schema.Record(Schema.String, MCP.Status),
15+
}).annotateMerge(
16+
OpenApi.annotations({
17+
identifier: "mcp.status",
18+
summary: "Get MCP status",
19+
description: "Get the status of all Model Context Protocol (MCP) servers.",
20+
}),
21+
),
22+
)
23+
.annotateMerge(
24+
OpenApi.annotations({
25+
title: "mcp",
26+
description: "Experimental HttpApi MCP routes.",
27+
}),
28+
),
29+
)
30+
.annotateMerge(
31+
OpenApi.annotations({
32+
title: "opencode experimental HttpApi",
33+
version: "0.0.1",
34+
description: "Experimental HttpApi surface for selected instance routes.",
35+
}),
36+
)
37+
38+
export const mcpHandlers = Layer.unwrap(
39+
Effect.gen(function* () {
40+
const mcp = yield* MCP.Service
41+
42+
const status = Effect.fn("McpHttpApi.status")(function* () {
43+
return yield* mcp.status()
44+
})
45+
46+
return HttpApiBuilder.group(McpApi, "mcp", (handlers) => handlers.handle("status", status))
47+
}),
48+
).pipe(Layer.provide(MCP.defaultLayer))

packages/opencode/src/server/routes/instance/httpapi/server.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { lazy } from "@/util/lazy"
1111
import { Filesystem } from "@/util"
1212
import { ConfigApi, configHandlers } from "./config"
1313
import { FileApi, fileHandlers } from "./file"
14+
import { McpApi, mcpHandlers } from "./mcp"
1415
import { PermissionApi, permissionHandlers } from "./permission"
1516
import { ProjectApi, projectHandlers } from "./project"
1617
import { ProviderApi, providerHandlers } from "./provider"
@@ -116,10 +117,12 @@ const ProviderSecured = ProviderApi.middleware(Authorization)
116117
const ConfigSecured = ConfigApi.middleware(Authorization)
117118
const WorkspaceSecured = WorkspaceApi.middleware(Authorization)
118119
const FileSecured = FileApi.middleware(Authorization)
120+
const McpSecured = McpApi.middleware(Authorization)
119121

120122
export const routes = Layer.mergeAll(
121123
HttpApiBuilder.layer(ConfigSecured).pipe(Layer.provide(configHandlers)),
122124
HttpApiBuilder.layer(FileSecured).pipe(Layer.provide(fileHandlers)),
125+
HttpApiBuilder.layer(McpSecured).pipe(Layer.provide(mcpHandlers)),
123126
HttpApiBuilder.layer(ProjectSecured).pipe(Layer.provide(projectHandlers)),
124127
HttpApiBuilder.layer(QuestionSecured).pipe(Layer.provide(questionHandlers)),
125128
HttpApiBuilder.layer(PermissionSecured).pipe(Layer.provide(permissionHandlers)),

packages/opencode/src/server/routes/instance/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { PermissionRoutes } from "./permission"
1717
import { Flag } from "@/flag/flag"
1818
import { ExperimentalHttpApiServer } from "./httpapi/server"
1919
import { FilePaths } from "./httpapi/file"
20+
import { McpPaths } from "./httpapi/mcp"
2021
import { ProjectRoutes } from "./project"
2122
import { SessionRoutes } from "./session"
2223
import { PtyRoutes } from "./pty"
@@ -52,6 +53,7 @@ export const InstanceRoutes = (upgrade: UpgradeWebSocket): Hono => {
5253
app.get(FilePaths.list, (c) => handler(c.req.raw, context))
5354
app.get(FilePaths.content, (c) => handler(c.req.raw, context))
5455
app.get(FilePaths.status, (c) => handler(c.req.raw, context))
56+
app.get(McpPaths.status, (c) => handler(c.req.raw, context))
5557
}
5658

5759
return app

packages/opencode/src/server/routes/instance/mcp.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export const McpRoutes = lazy(() =>
2121
description: "MCP server status",
2222
content: {
2323
"application/json": {
24-
schema: resolver(z.record(z.string(), MCP.Status)),
24+
schema: resolver(z.record(z.string(), MCP.Status.zod)),
2525
},
2626
},
2727
},
@@ -44,7 +44,7 @@ export const McpRoutes = lazy(() =>
4444
description: "MCP server added successfully",
4545
content: {
4646
"application/json": {
47-
schema: resolver(z.record(z.string(), MCP.Status)),
47+
schema: resolver(z.record(z.string(), MCP.Status.zod)),
4848
},
4949
},
5050
},
@@ -121,7 +121,7 @@ export const McpRoutes = lazy(() =>
121121
description: "OAuth authentication completed",
122122
content: {
123123
"application/json": {
124-
schema: resolver(MCP.Status),
124+
schema: resolver(MCP.Status.zod),
125125
},
126126
},
127127
},
@@ -153,7 +153,7 @@ export const McpRoutes = lazy(() =>
153153
description: "OAuth authentication completed",
154154
content: {
155155
"application/json": {
156-
schema: resolver(MCP.Status),
156+
schema: resolver(MCP.Status.zod),
157157
},
158158
},
159159
},
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { afterEach, describe, expect, test } from "bun:test"
2+
import { Context } from "effect"
3+
import { ExperimentalHttpApiServer } from "../../src/server/routes/instance/httpapi/server"
4+
import { McpPaths } from "../../src/server/routes/instance/httpapi/mcp"
5+
import { Instance } from "../../src/project/instance"
6+
import { Log } from "../../src/util"
7+
import { resetDatabase } from "../fixture/db"
8+
import { tmpdir } from "../fixture/fixture"
9+
10+
void Log.init({ print: false })
11+
12+
const context = Context.empty() as Context.Context<unknown>
13+
14+
function request(route: string, directory: string) {
15+
return ExperimentalHttpApiServer.webHandler().handler(
16+
new Request(`http://localhost${route}`, {
17+
headers: {
18+
"x-opencode-directory": directory,
19+
},
20+
}),
21+
context,
22+
)
23+
}
24+
25+
afterEach(async () => {
26+
await Instance.disposeAll()
27+
await resetDatabase()
28+
})
29+
30+
describe("mcp HttpApi", () => {
31+
test("serves status endpoint", async () => {
32+
await using tmp = await tmpdir({
33+
config: {
34+
mcp: {
35+
demo: {
36+
type: "local",
37+
command: ["echo", "demo"],
38+
enabled: false,
39+
},
40+
},
41+
},
42+
})
43+
44+
const response = await request(McpPaths.status, tmp.path)
45+
expect(response.status).toBe(200)
46+
expect(await response.json()).toEqual({ demo: { status: "disabled" } })
47+
})
48+
})

0 commit comments

Comments
 (0)