forked from anomalyco/opencode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttpapi-mcp.test.ts
More file actions
186 lines (164 loc) · 5.82 KB
/
httpapi-mcp.test.ts
File metadata and controls
186 lines (164 loc) · 5.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import { afterEach, describe, expect, test } from "bun:test"
import { Context, Effect, FileSystem, Layer, Path } from "effect"
import { NodeFileSystem, NodePath } from "@effect/platform-node"
import { Flag } from "@opencode-ai/core/flag/flag"
import { ExperimentalHttpApiServer } from "../../src/server/routes/instance/httpapi/server"
import { McpPaths } from "../../src/server/routes/instance/httpapi/groups/mcp"
import { Instance } from "../../src/project/instance"
import { Server } from "../../src/server/server"
import * as Log from "@opencode-ai/core/util/log"
import { resetDatabase } from "../fixture/db"
import { provideInstance, tmpdir } from "../fixture/fixture"
import { testEffect } from "../lib/effect"
void Log.init({ print: false })
const original = Flag.OPENCODE_EXPERIMENTAL_HTTPAPI
const context = Context.empty() as Context.Context<unknown>
const it = testEffect(Layer.mergeAll(NodeFileSystem.layer, NodePath.layer))
function app(experimental: boolean) {
Flag.OPENCODE_EXPERIMENTAL_HTTPAPI = experimental
return experimental ? Server.Default().app : Server.Legacy().app
}
type TestApp = ReturnType<typeof app>
function request(route: string, directory: string, init?: RequestInit) {
const headers = new Headers(init?.headers)
headers.set("x-opencode-directory", directory)
return ExperimentalHttpApiServer.webHandler().handler(
new Request(`http://localhost${route}`, {
...init,
headers,
}),
context,
)
}
function withMcpProject<A, E, R>(self: (dir: string) => Effect.Effect<A, E, R>) {
return Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem
const path = yield* Path.Path
const dir = yield* fs.makeTempDirectoryScoped({ prefix: "opencode-test-" })
yield* fs.writeFileString(
path.join(dir, "opencode.json"),
JSON.stringify({
$schema: "https://opencode.ai/config.json",
formatter: false,
lsp: false,
mcp: {
demo: {
type: "local",
command: ["echo", "demo"],
enabled: false,
},
},
}),
)
yield* Effect.addFinalizer(() =>
Effect.promise(() => Instance.provide({ directory: dir, fn: () => Instance.dispose() })).pipe(Effect.ignore),
)
return yield* self(dir).pipe(provideInstance(dir))
})
}
const readResponse = Effect.fnUntraced(function* (input: { app: TestApp; path: string; headers: HeadersInit }) {
const response = yield* Effect.promise(() =>
Promise.resolve(input.app.request(input.path, { method: "POST", headers: input.headers })),
)
return {
status: response.status,
body: yield* Effect.promise(() => response.text()),
}
})
afterEach(async () => {
Flag.OPENCODE_EXPERIMENTAL_HTTPAPI = original
await Instance.disposeAll()
await resetDatabase()
})
describe("mcp HttpApi", () => {
test("serves status endpoint", async () => {
await using tmp = await tmpdir({
config: {
mcp: {
demo: {
type: "local",
command: ["echo", "demo"],
enabled: false,
},
},
},
})
const response = await request(McpPaths.status, tmp.path)
expect(response.status).toBe(200)
expect(await response.json()).toEqual({ demo: { status: "disabled" } })
})
test("serves add, connect, and disconnect endpoints", async () => {
await using tmp = await tmpdir({
config: {
mcp: {
demo: {
type: "local",
command: ["echo", "demo"],
enabled: false,
},
},
},
})
const added = await request(McpPaths.status, tmp.path, {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({
name: "added",
config: {
type: "local",
command: ["echo", "added"],
enabled: false,
},
}),
})
expect(added.status).toBe(200)
expect(await added.json()).toMatchObject({ added: { status: "disabled" } })
const connected = await request("/mcp/demo/connect", tmp.path, { method: "POST" })
expect(connected.status).toBe(200)
expect(await connected.json()).toBe(true)
const disconnected = await request("/mcp/demo/disconnect", tmp.path, { method: "POST" })
expect(disconnected.status).toBe(200)
expect(await disconnected.json()).toBe(true)
})
test("serves deterministic OAuth endpoints", async () => {
await using tmp = await tmpdir({
config: {
mcp: {
demo: {
type: "local",
command: ["echo", "demo"],
enabled: false,
},
},
},
})
const start = await request("/mcp/demo/auth", tmp.path, { method: "POST" })
expect(start.status).toBe(400)
const authenticate = await request("/mcp/demo/auth/authenticate", tmp.path, { method: "POST" })
expect(authenticate.status).toBe(400)
const removed = await request("/mcp/demo/auth", tmp.path, { method: "DELETE" })
expect(removed.status).toBe(200)
expect(await removed.json()).toEqual({ success: true })
})
it.live(
"matches legacy unsupported OAuth error responses",
withMcpProject((dir) =>
Effect.gen(function* () {
const headers = { "x-opencode-directory": dir }
const legacy = app(false)
const httpapi = app(true)
yield* Effect.forEach(["/mcp/demo/auth", "/mcp/demo/auth/authenticate"], (path) =>
Effect.gen(function* () {
const legacyResponse = yield* readResponse({ app: legacy, path, headers })
const httpapiResponse = yield* readResponse({ app: httpapi, path, headers })
expect(legacyResponse).toEqual({
status: 400,
body: JSON.stringify({ error: "MCP server demo does not support OAuth" }),
})
expect(httpapiResponse).toEqual(legacyResponse)
}),
)
}),
),
)
})