mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-19 08:39:02 +03:00
fix(core): initialize MCP session for TinyFish web search (#54730)
Co-authored-by: Daniel Smolsky <dannysmo@gmail.com>
This commit is contained in:
committed by
GitHub
Unverified
parent
68510d9d6e
commit
86755e1cea
@@ -1,9 +1,10 @@
|
||||
export * as WebSearchTinyFish from "./tinyfish.js"
|
||||
|
||||
import { define } from "@opencode/plugin/effect/plugin"
|
||||
import { Effect, Option, Schema, Scope } from "effect"
|
||||
import { HttpClient } from "effect/http"
|
||||
import { Duration, Effect, Option, Schema, Scope } from "effect"
|
||||
import { HttpClient, HttpClientRequest } from "effect/http"
|
||||
import { App } from "../../app.js"
|
||||
import { collectBoundedResponseBody } from "../../tool/http-body.js"
|
||||
import { WebSearchMcp } from "./mcp.js"
|
||||
|
||||
export const endpoint = "https://agent.tinyfish.ai/mcp"
|
||||
@@ -29,6 +30,22 @@ const SearchResponse = Schema.fromJsonString(
|
||||
)
|
||||
const decodeSearchResponse = Schema.decodeUnknownOption(SearchResponse)
|
||||
|
||||
const InitializeRequest = Schema.Struct({
|
||||
jsonrpc: Schema.Literal("2.0"),
|
||||
id: Schema.Literal(1),
|
||||
method: Schema.Literal("initialize"),
|
||||
params: Schema.Struct({
|
||||
protocolVersion: Schema.String,
|
||||
capabilities: Schema.Struct({}),
|
||||
clientInfo: Schema.Struct({ name: Schema.String, version: Schema.String }),
|
||||
}),
|
||||
})
|
||||
|
||||
const InitializedNotification = Schema.Struct({
|
||||
jsonrpc: Schema.Literal("2.0"),
|
||||
method: Schema.Literal("notifications/initialized"),
|
||||
})
|
||||
|
||||
export const Plugin = define<HttpClient.HttpClient | Scope.Scope>({
|
||||
id: "opencode.websearch.tinyfish",
|
||||
effect: Effect.fn("WebSearchTinyFish.Plugin")(function* (ctx) {
|
||||
@@ -52,18 +69,21 @@ export const Plugin = define<HttpClient.HttpClient | Scope.Scope>({
|
||||
Effect.gen(function* () {
|
||||
const connection = yield* ctx.integration.connection.active("tinyfish")
|
||||
const credential = connection ? yield* ctx.integration.connection.resolve(connection) : undefined
|
||||
const headers = {
|
||||
"User-Agent": App.useragent(ctx.app),
|
||||
...(credential?.type === "key"
|
||||
? { "X-API-Key": credential.key }
|
||||
: { "X-TinyFish-Access-Mode": "keyless" }),
|
||||
}
|
||||
// Keyed requests are rejected without the `Mcp-Session-Id` returned by `initialize`; keyless ones are not.
|
||||
const session = credential?.type === "key" ? yield* initialize(http, headers) : undefined
|
||||
const result = yield* WebSearchMcp.call(
|
||||
http,
|
||||
endpoint,
|
||||
"search",
|
||||
{ input: McpInput, output: McpOutput },
|
||||
{ query: input.query },
|
||||
{
|
||||
"User-Agent": App.useragent(ctx.app),
|
||||
...(credential?.type === "key"
|
||||
? { "X-API-Key": credential.key }
|
||||
: { "X-TinyFish-Access-Mode": "keyless" }),
|
||||
},
|
||||
{ ...headers, ...(session === undefined ? {} : { "Mcp-Session-Id": session }) },
|
||||
)
|
||||
const content = result?.content.find((item) => item.text)
|
||||
const response = content ? Option.getOrUndefined(decodeSearchResponse(content.text)) : undefined
|
||||
@@ -80,3 +100,47 @@ export const Plugin = define<HttpClient.HttpClient | Scope.Scope>({
|
||||
})
|
||||
}),
|
||||
})
|
||||
|
||||
const initialize = (http: HttpClient.HttpClient, headers: Record<string, string>) =>
|
||||
Effect.gen(function* () {
|
||||
const client = HttpClient.withScope(HttpClient.filterStatusOk(http))
|
||||
const request = yield* HttpClientRequest.post(endpoint).pipe(
|
||||
HttpClientRequest.accept("application/json, text/event-stream"),
|
||||
HttpClientRequest.setHeaders(headers),
|
||||
HttpClientRequest.schemaBodyJson(InitializeRequest)({
|
||||
jsonrpc: "2.0",
|
||||
id: 1,
|
||||
method: "initialize",
|
||||
params: {
|
||||
protocolVersion: "2025-06-18",
|
||||
capabilities: {},
|
||||
clientInfo: { name: "opencode", version: "1.0" },
|
||||
},
|
||||
}),
|
||||
)
|
||||
const response = yield* client.execute(request)
|
||||
yield* collectBoundedResponseBody(
|
||||
response,
|
||||
WebSearchMcp.MAX_RESPONSE_BYTES,
|
||||
() => new Error(`initialize response exceeded ${WebSearchMcp.MAX_RESPONSE_BYTES} bytes`),
|
||||
)
|
||||
const session = response.headers["mcp-session-id"]
|
||||
if (session === undefined)
|
||||
return yield* Effect.fail(new Error("TinyFish MCP initialize did not return an Mcp-Session-Id header"))
|
||||
const notification = yield* HttpClientRequest.post(endpoint).pipe(
|
||||
HttpClientRequest.accept("application/json, text/event-stream"),
|
||||
HttpClientRequest.setHeaders({ ...headers, "Mcp-Session-Id": session }),
|
||||
HttpClientRequest.schemaBodyJson(InitializedNotification)({
|
||||
jsonrpc: "2.0",
|
||||
method: "notifications/initialized",
|
||||
}),
|
||||
)
|
||||
yield* client.execute(notification)
|
||||
return session
|
||||
}).pipe(
|
||||
Effect.scoped,
|
||||
Effect.timeoutOrElse({
|
||||
duration: Duration.seconds(25),
|
||||
orElse: () => Effect.fail(new Error("TinyFish initialize request timed out")),
|
||||
}),
|
||||
)
|
||||
|
||||
@@ -20,12 +20,18 @@ export const requests: WebSearchRequest[] = []
|
||||
export const signals: AbortSignal[] = []
|
||||
let responseBody = ""
|
||||
let responseStatus = 200
|
||||
let responseHeaders: Record<string, string> = {}
|
||||
|
||||
export function resetWebSearchFixture(body: string, status = 200) {
|
||||
requests.length = 0
|
||||
signals.length = 0
|
||||
responseBody = body
|
||||
responseStatus = status
|
||||
responseHeaders = {}
|
||||
}
|
||||
|
||||
export function setResponseHeaders(headers: Record<string, string>) {
|
||||
responseHeaders = headers
|
||||
}
|
||||
|
||||
const http = Layer.succeed(
|
||||
@@ -39,7 +45,10 @@ const http = Layer.succeed(
|
||||
headers: request.headers,
|
||||
body: JSON.parse(new TextDecoder().decode(request.body.body)),
|
||||
})
|
||||
return HttpClientResponse.fromWeb(request, new Response(responseBody, { status: responseStatus }))
|
||||
return HttpClientResponse.fromWeb(
|
||||
request,
|
||||
new Response(responseBody, { status: responseStatus, headers: responseHeaders }),
|
||||
)
|
||||
}),
|
||||
),
|
||||
)
|
||||
|
||||
@@ -8,7 +8,13 @@ import { WebSearchParallel } from "@opencode/core/plugin/websearch/parallel"
|
||||
import { WebSearchTavily } from "@opencode/core/plugin/websearch/tavily"
|
||||
import { WebSearchTinyFish } from "@opencode/core/plugin/websearch/tinyfish"
|
||||
import { host, integrationHost, webSearchHost } from "./host"
|
||||
import { requests, signals, resetWebSearchFixture, webSearchIntegrationTest } from "./websearch-fixture"
|
||||
import {
|
||||
requests,
|
||||
signals,
|
||||
resetWebSearchFixture,
|
||||
setResponseHeaders,
|
||||
webSearchIntegrationTest,
|
||||
} from "./websearch-fixture"
|
||||
|
||||
beforeEach(() => {
|
||||
resetWebSearchFixture(
|
||||
@@ -353,11 +359,15 @@ describe("built-in web search providers", () => {
|
||||
integrationID: Integration.ID.make("tinyfish"),
|
||||
key: "tinyfish-secret",
|
||||
})
|
||||
setResponseHeaders({ "mcp-session-id": "ses_tinyfish" })
|
||||
yield* websearch.query({ query: "effect typescript", providerID: WebSearch.ID.make("tinyfish") })
|
||||
expect(requests[1]).toMatchObject({
|
||||
headers: { "x-api-key": "tinyfish-secret" },
|
||||
expect(requests[1]?.body).toMatchObject({ method: "initialize" })
|
||||
expect(requests[2]?.body).toMatchObject({ method: "notifications/initialized" })
|
||||
expect(requests[3]).toMatchObject({
|
||||
headers: { "x-api-key": "tinyfish-secret", "mcp-session-id": "ses_tinyfish" },
|
||||
body: { method: "tools/call" },
|
||||
})
|
||||
expect(requests[1]?.headers["x-tinyfish-access-mode"]).toBeUndefined()
|
||||
expect(requests[3]?.headers["x-tinyfish-access-mode"]).toBeUndefined()
|
||||
}),
|
||||
)
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user