httpapi-mcp.test.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. import { describe, expect } from "bun:test"
  2. import { Context, Effect, Layer } from "effect"
  3. import { HttpApiApp } from "../../src/server/routes/instance/httpapi/server"
  4. import { McpPaths } from "../../src/server/routes/instance/httpapi/groups/mcp"
  5. import { Server } from "../../src/server/server"
  6. import { resetDatabase } from "../fixture/db"
  7. import { TestInstance } from "../fixture/fixture"
  8. import { testEffect } from "../lib/effect"
  9. const context = Context.empty() as Context.Context<unknown>
  10. const testStateLayer = Layer.effectDiscard(
  11. Effect.gen(function* () {
  12. yield* Effect.promise(() => resetDatabase())
  13. yield* Effect.addFinalizer(() => Effect.promise(() => resetDatabase()).pipe(Effect.ignore))
  14. }),
  15. )
  16. const it = testEffect(testStateLayer)
  17. function app() {
  18. return Server.Default().app
  19. }
  20. type TestApp = ReturnType<typeof app>
  21. type TestHandler = ReturnType<typeof HttpApiApp.webHandler>
  22. const request = Effect.fnUntraced(function* (
  23. handler: TestHandler,
  24. route: string,
  25. directory: string,
  26. init?: RequestInit,
  27. ) {
  28. const headers = new Headers(init?.headers)
  29. headers.set("x-opencode-directory", directory)
  30. return yield* Effect.promise(() =>
  31. Promise.resolve(
  32. handler.handler(
  33. new Request(`http://localhost${route}`, {
  34. ...init,
  35. headers,
  36. }),
  37. context,
  38. ),
  39. ),
  40. )
  41. })
  42. const json = <A>(response: Response) => Effect.promise(() => response.json() as Promise<A>)
  43. const readResponse = Effect.fnUntraced(function* (input: { app: TestApp; path: string; headers: HeadersInit }) {
  44. const response = yield* Effect.promise(() =>
  45. Promise.resolve(input.app.request(input.path, { method: "POST", headers: input.headers })),
  46. )
  47. return {
  48. status: response.status,
  49. body: yield* Effect.promise(() => response.text()),
  50. }
  51. })
  52. describe("mcp HttpApi", () => {
  53. it.instance(
  54. "serves status endpoint",
  55. () =>
  56. Effect.gen(function* () {
  57. const tmp = yield* TestInstance
  58. const handler = HttpApiApp.webHandler()
  59. const response = yield* request(handler, McpPaths.status, tmp.directory)
  60. expect(response.status).toBe(200)
  61. expect(yield* json(response)).toEqual({ demo: { status: "disabled" } })
  62. }),
  63. {
  64. config: {
  65. mcp: {
  66. demo: {
  67. type: "local",
  68. command: ["echo", "demo"],
  69. enabled: false,
  70. },
  71. },
  72. },
  73. },
  74. )
  75. it.instance(
  76. "serves add, connect, and disconnect endpoints",
  77. () =>
  78. Effect.gen(function* () {
  79. const tmp = yield* TestInstance
  80. const handler = HttpApiApp.webHandler()
  81. const added = yield* request(handler, McpPaths.status, tmp.directory, {
  82. method: "POST",
  83. headers: { "content-type": "application/json" },
  84. body: JSON.stringify({
  85. name: "added",
  86. config: {
  87. type: "local",
  88. command: ["echo", "added"],
  89. enabled: false,
  90. },
  91. }),
  92. })
  93. expect(added.status).toBe(200)
  94. expect(yield* json(added)).toMatchObject({ added: { status: "disabled" } })
  95. const addedDisconnected = yield* request(handler, "/mcp/added/disconnect", tmp.directory, { method: "POST" })
  96. expect(addedDisconnected.status).toBe(200)
  97. expect(yield* json(addedDisconnected)).toBe(true)
  98. const connected = yield* request(handler, "/mcp/demo/connect", tmp.directory, { method: "POST" })
  99. expect(connected.status).toBe(200)
  100. expect(yield* json(connected)).toBe(true)
  101. const disconnected = yield* request(handler, "/mcp/demo/disconnect", tmp.directory, { method: "POST" })
  102. expect(disconnected.status).toBe(200)
  103. expect(yield* json(disconnected)).toBe(true)
  104. }),
  105. {
  106. config: {
  107. mcp: {
  108. demo: {
  109. type: "local",
  110. command: ["echo", "demo"],
  111. enabled: false,
  112. },
  113. },
  114. },
  115. },
  116. )
  117. it.instance(
  118. "serves deterministic OAuth endpoints",
  119. () =>
  120. Effect.gen(function* () {
  121. const tmp = yield* TestInstance
  122. const handler = HttpApiApp.webHandler()
  123. const start = yield* request(handler, "/mcp/demo/auth", tmp.directory, { method: "POST" })
  124. expect(start.status).toBe(400)
  125. const authenticate = yield* request(handler, "/mcp/demo/auth/authenticate", tmp.directory, { method: "POST" })
  126. expect(authenticate.status).toBe(400)
  127. const removed = yield* request(handler, "/mcp/demo/auth", tmp.directory, { method: "DELETE" })
  128. expect(removed.status).toBe(200)
  129. expect(yield* json(removed)).toEqual({ success: true })
  130. }),
  131. {
  132. config: {
  133. mcp: {
  134. demo: {
  135. type: "local",
  136. command: ["echo", "demo"],
  137. enabled: false,
  138. },
  139. },
  140. },
  141. },
  142. )
  143. it.instance(
  144. "returns unsupported OAuth error responses",
  145. () =>
  146. Effect.gen(function* () {
  147. const tmp = yield* TestInstance
  148. const dir = tmp.directory
  149. const headers = { "x-opencode-directory": dir }
  150. yield* Effect.forEach(["/mcp/demo/auth", "/mcp/demo/auth/authenticate"], (path) =>
  151. Effect.gen(function* () {
  152. const response = yield* readResponse({ app: app(), path, headers })
  153. expect(response).toEqual({
  154. status: 400,
  155. body: JSON.stringify({ error: "MCP server demo does not support OAuth" }),
  156. })
  157. }),
  158. )
  159. }),
  160. {
  161. config: {
  162. formatter: false,
  163. lsp: false,
  164. mcp: {
  165. demo: {
  166. type: "local",
  167. command: ["echo", "demo"],
  168. enabled: false,
  169. },
  170. },
  171. },
  172. },
  173. )
  174. it.instance(
  175. "returns typed not found errors for missing MCP servers",
  176. () =>
  177. Effect.gen(function* () {
  178. const tmp = yield* TestInstance
  179. const handler = HttpApiApp.webHandler()
  180. for (const input of [
  181. { method: "POST", route: "/mcp/missing/auth" },
  182. { method: "POST", route: "/mcp/missing/auth/authenticate" },
  183. { method: "POST", route: "/mcp/missing/auth/callback", body: JSON.stringify({ code: "code" }) },
  184. { method: "DELETE", route: "/mcp/missing/auth" },
  185. { method: "POST", route: "/mcp/missing/connect" },
  186. { method: "POST", route: "/mcp/missing/disconnect" },
  187. ]) {
  188. const response = yield* request(handler, input.route, tmp.directory, {
  189. method: input.method,
  190. headers: input.body ? { "content-type": "application/json" } : undefined,
  191. body: input.body,
  192. })
  193. expect(response.status).toBe(404)
  194. expect(yield* json(response)).toEqual({
  195. _tag: "McpServerNotFoundError",
  196. name: "missing",
  197. message: "MCP server not found: missing",
  198. })
  199. }
  200. }),
  201. { config: { mcp: {} } },
  202. )
  203. })