Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion packages/opencode/src/mcp/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,12 @@ const layer = Layer.effect(

if (!mcpClient) {
if (status.status !== "connected" && status.status !== "disabled") {
yield* Effect.logWarning("server unavailable", { key, type: mcp.type, status: status.status })
yield* Effect.logWarning("server unavailable", {
key,
type: mcp.type,
status: status.status,
error: "error" in status ? status.error : undefined,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: The newly logged status.error is raw error.message from the connect path. For local servers this can embed subprocess/stderr text, and for remote servers it can embed URL or server-returned content, so a server or misconfigured URL containing credentials would now dump them into logs. Mask/sanitize the error string (strip URLs' userinfo, cap length) before logging, or verify none of these transports place credentials in error.message.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At packages/opencode/src/mcp/index.ts, line 406:

<comment>The newly logged `status.error` is raw `error.message` from the connect path. For local servers this can embed subprocess/stderr text, and for remote servers it can embed URL or server-returned content, so a server or misconfigured URL containing credentials would now dump them into logs. Mask/sanitize the error string (strip URLs' userinfo, cap length) before logging, or verify none of these transports place credentials in `error.message`.</comment>

<file context>
@@ -399,7 +399,12 @@ const layer = Layer.effect(
+              key,
+              type: mcp.type,
+              status: status.status,
+              error: "error" in status ? status.error : undefined,
+            })
           }
</file context>

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P3: For the needs_auth status the log line now carries an error: undefined field, which is noise in exactly the log this PR is improving. Spread the field conditionally so it is absent rather than undefined when the status has no error.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At packages/opencode/src/mcp/index.ts, line 406:

<comment>For the `needs_auth` status the log line now carries an `error: undefined` field, which is noise in exactly the log this PR is improving. Spread the field conditionally so it is absent rather than `undefined` when the status has no error.</comment>

<file context>
@@ -399,7 +399,12 @@ const layer = Layer.effect(
+              key,
+              type: mcp.type,
+              status: status.status,
+              error: "error" in status ? status.error : undefined,
+            })
           }
</file context>
Suggested change
error: "error" in status ? status.error : undefined,
...("error" in status ? { error: status.error } : {}),

})
}
return { status } satisfies CreateResult
}
Expand Down
26 changes: 25 additions & 1 deletion packages/opencode/test/mcp/lifecycle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
type Tool,
} from "@modelcontextprotocol/sdk/types.js"
import { LayerNode } from "@opencode-ai/core/effect/layer-node"
import { Cause, Effect, Exit } from "effect"
import { Cause, Effect, Exit, Logger } from "effect"
import type { MCP as MCPNS } from "../../src/mcp/index"
import { MCP } from "../../src/mcp/index"
import { McpOAuthCallback } from "../../src/mcp/oauth-callback"
Expand Down Expand Up @@ -493,6 +493,30 @@ it.instance("unavailable remote server is marked failed without tools", () =>
}),
)

it.instance("unavailable remote server surfaces the underlying error in the log", () =>
Effect.gen(function* () {
const server = yield* Effect.acquireRelease(
Effect.sync(() => Bun.serve({ port: 0, fetch: () => new Response("unavailable", { status: 503 }) })),
(http) => Effect.promise(() => http.stop(true)),
)
const mcp = yield* MCP.Service
const captured: Array<unknown> = []
const capture = Logger.make((options) => {
captured.push(options.message)
})

yield* mcp
.add("unlogged", remote(server.url.toString(), 500))
.pipe(Effect.provide(Logger.layer([capture], { mergeWithExisting: true })))

const logged = captured.find((item) => Array.isArray(item) && item[0] === "server unavailable")
expect(logged).toBeDefined()
if (!Array.isArray(logged)) return
const fields: unknown = logged[1]
expect(fields).toMatchObject({ status: "failed", error: expect.any(String) })
}),
)

it.instance("tools() prefixes sanitized server and tool names", () =>
Effect.gen(function* () {
const server = yield* lifecycleServer({ capabilities: { tools: {} } })
Expand Down
Loading