Skip to content
Open
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
5 changes: 5 additions & 0 deletions .changeset/tall-mirrors-provide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"agents": patch
---

fix mcp: improve error code detection when listing tools fails
15 changes: 11 additions & 4 deletions packages/agents/src/mcp/client-connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { Emitter, type Event } from "../core/events";
import type { MCPObservabilityEvent } from "../observability/mcp";
import type { AgentsOAuthProvider } from "./do-oauth-client-provider";
import {
getErrorCode,
isTransportNotImplemented,
isUnauthorized,
toErrorMessage
Expand Down Expand Up @@ -663,17 +664,23 @@ export class MCPClientConnection {
}

private _capabilityErrorHandler<T>(empty: T, method: string) {
return (e: { code: number }) => {
// server is badly behaved and returning invalid capabilities. This commonly occurs for resource templates
if (e.code === -32601) {
return (e: unknown) => {
// Server is badly behaved and returning invalid capabilities. This commonly occurs for resource templates.
// Check both the error code property and the error message for -32601 (Method not found)
const errorCode = getErrorCode(e);
const errorMessage = toErrorMessage(e);
const isMethodNotFound =
errorCode === -32601 || errorMessage.includes('"code":-32601');
Copy link
Contributor

Choose a reason for hiding this comment

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

On second thoughts this feels like it will be too broad. Do you have an example of a server which breaks this? I'd prefer to fix this on the MCP SDK side.


if (isMethodNotFound) {
const url = this.url.toString();
this._onObservabilityEvent.fire({
type: "mcp:client:discover",
displayMessage: `The server advertised support for the capability ${method.split("/")[0]}, but returned "Method not found" for '${method}' for ${url}`,
payload: {
url,
capability: method.split("/")[0],
error: toErrorMessage(e)
error: errorMessage
},
timestamp: Date.now(),
id: nanoid()
Expand Down
2 changes: 1 addition & 1 deletion packages/agents/src/mcp/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export function toErrorMessage(error: unknown): string {
return error instanceof Error ? error.message : String(error);
}

function getErrorCode(error: unknown): number | undefined {
export function getErrorCode(error: unknown): number | undefined {
if (
error &&
typeof error === "object" &&
Expand Down
Loading