ai icon indicating copy to clipboard operation
ai copied to clipboard

@ai-sdk/mcp: export MCPClientError so callers can use isInstance and statusCode

Open ruiined opened this issue 2 months ago • 4 comments

Description

MCPClientError is not exported from @ai-sdk/mcp, at runtime or in its types. #15605 added statusCode, url and responseBody to it so that downstream consumers can "branch on HTTP context structurally", but there is no supported way to recognize the error before reading those fields. MCPClientError.isInstance is unreachable, and AISDKError.hasMarker is protected.

The package already exports its other error classes (UnauthorizedError, MCPClientOAuthError, AuthorizationServerMismatchError), so this one looks like an omission.

Our use case: retry createMCPClient on transient transport failures (5xx, timeouts, connection closed), but not on a 401/403 that will only be rejected again. Today that means reading the brand directly, which depends on an internal detail:

const MCP_CLIENT_ERROR_MARKER = Symbol.for('vercel.ai.error.AI_MCPClientError')

const isMcpClientError = (err: object) =>
  Reflect.get(err, MCP_CLIENT_ERROR_MARKER) === true

Expected: import { MCPClientError } from '@ai-sdk/mcp' works, so callers can use MCPClientError.isInstance(error) and get a typed statusCode.

Actual: the import fails, and statusCode is missing from the published types even though it is set on the instance.

Reproduction

import { createMCPClient, MCPClientError } from '@ai-sdk/mcp'
// TS2305: Module '"@ai-sdk/mcp"' has no exported member 'MCPClientError'.

try {
  await createMCPClient({ transport: { type: 'http', url: 'https://example.com/returns-403' } })
} catch (error) {
  if (MCPClientError.isInstance(error) && error.statusCode === 403) {
    // don't retry
  }
}

AI SDK Version

  • @ai-sdk/mcp: 2.0.55 (also checked main at 2.0.66: packages/mcp/src/index.ts does not export it)

Code of Conduct

  • [x] I agree to follow this project's Code of Conduct

ruiined avatar Oct 02 '26 14:10 ruiined

Identify issue type

Identified as Feature
Confidence: High

Rationale

This requests an expansion of the package’s public API by exporting an currently internal error class and its types. The failed import reflects a capability that does not yet exist rather than a broken supported export.

Evidence

  • MCPClientError exists internally but is not exported by @ai-sdk/mcp.
  • The request is to expose the class publicly so callers can use MCPClientError.isInstance(error) and access typed HTTP context such as statusCode.
  • No existing documented public export is shown as regressed or malfunctioning.

ai-sdk-factory[bot] avatar Oct 02 '26 14:10 ai-sdk-factory[bot]

Feature Analysis

Feature is not available.

Feature Description

  • Allow callers to import MCPClientError from @ai-sdk/mcp, use MCPClientError.isInstance(error) to narrow unknown errors, and access typed optional fields including code, statusCode, url, and responseBody for transport and retry decisions.
  • Current main defines MCPClientError and its HTTP metadata in packages/mcp/src/error/mcp-client-error.ts, but packages/mcp/src/index.ts does not export it; the built runtime exposes the other MCP error classes but not MCPClientError, dist/index.d.ts omits it, and the retained probe packages/mcp/issue-21963-mcp-client-error-export-probe.ts fails with TS2305.
  • The public export and typed metadata are coverage dimensions of one error-handling contract rather than independently shippable features: exposing the class is what makes its existing isInstance guard and fields usable by consumers.

AI SDK Fit

  • The feature aligns with the documented AI SDK error-handling pattern, which recommends specific static ErrorClass.isInstance guards over instanceof or message matching.
  • It improves API consistency because @ai-sdk/mcp already publicly exports UnauthorizedError, MCPClientOAuthError, and AuthorizationServerMismatchError while its createMCPClient documentation states that MCPClientError is thrown.
  • This is a focused additive API change within the MCP building block and does not introduce a new cross-provider abstraction or expand the core AI SDK mission.

Implications / Considerations

  • Implementation is primarily limited to @ai-sdk/mcp: export MCPClientError from packages/mcp/src/index.ts so the package build emits it from both dist/index.js and dist/index.d.ts; no model provider integration or shared provider abstraction needs modification.
  • No MCP, JSON-RPC, HTTP, or provider specification change is required because the class, marker-based isInstance guard, and metadata fields already exist and are populated by current transport code.
  • The public ai package does not need a new or changed API because createMCPClient and its errors belong to the separate @ai-sdk/mcp package.
  • The change is additive and should be backward compatible, but exporting the class makes its name, marker behavior, fields, and constructor surface a supported contract that future releases must preserve or evolve compatibly.
  • AI SDK UI message types, framework integrations, and user-facing UI behavior are unaffected because the error is raised by MCP client initialization and transport operations outside the UI protocol.
  • Tests should verify the root runtime export, TypeScript import, isInstance narrowing, typed optional metadata, and representative HTTP, network, timeout, and connection-closed failures in the supported Node and Edge test configurations.
  • Documentation should make the existing createMCPClient error section actionable with an import and isInstance example, describe when statusCode, url, and responseBody are undefined, and clarify that maxRetries only retries tools/call operations rather than createMCPClient initialization.
  • Error-handling guidance should account for related exported errors: some OAuth authorization failures can surface UnauthorizedError or MCPClientOAuthError rather than MCPClientError, while failures without an HTTP response cannot provide statusCode.
  • Examples and documentation should warn against indiscriminately logging url or responseBody because endpoint URLs, query parameters, and server response content may contain sensitive information; exporting the class does not itself expose data that is not already present at runtime.

Relevant Documentation

ai-sdk-factory[bot] avatar Oct 02 '26 14:10 ai-sdk-factory[bot]

Feature implementation

Created feature implementation and opened PR: #21964

Summary

Exported MCPClientError from @ai-sdk/mcp, including its isInstance type guard and existing optional code, statusCode, url, and responseBody metadata.

Testing

Added runtime coverage for the package-root export and compile-time coverage for error narrowing and metadata types across Node and Edge tests.

End-to-end Validation

  • Added and ran a local HTTP MCP example that classifies a 503 initialization failure with MCPClientError.isInstance and statusCode.
  • Built @ai-sdk/mcp and confirmed the generated runtime and declaration exports.

Documentation

Updated the MCP guide and createMCPClient reference with typed error-handling, retry scope, metadata availability, and sensitive-data logging guidance.

ai-sdk-factory[bot] avatar Oct 02 '26 15:10 ai-sdk-factory[bot]

:rocket: Published in:

Package Version
ai 7.0.128 github npm
@ai-sdk/alibaba 2.0.61 github npm
@ai-sdk/amazon-bedrock 5.0.106 github npm
@ai-sdk/angular 3.0.128 github npm
@ai-sdk/anthropic 4.0.72 github npm
@ai-sdk/anthropic-aws 2.0.64 github npm
@ai-sdk/assemblyai 3.0.54 github npm
@ai-sdk/azure 4.0.91 github npm
@ai-sdk/baseten 2.1.41 github npm
@ai-sdk/black-forest-labs 2.0.55 github npm
@ai-sdk/bytedance 2.0.57 github npm
@ai-sdk/cartesia 3.0.49 github npm
@ai-sdk/cerebras 3.0.63 github npm
@ai-sdk/code-mode 1.0.85 github npm
@ai-sdk/cohere 4.0.55 github npm
@ai-sdk/deepgram 3.1.25 github npm
@ai-sdk/deepinfra 3.0.63 github npm
@ai-sdk/deepseek 3.0.59 github npm
@ai-sdk/devtools 1.0.31 github npm
@ai-sdk/elevenlabs 3.0.55 github npm
@ai-sdk/fal 3.0.55 github npm
@ai-sdk/fireworks 3.0.66 github npm
@ai-sdk/fish-audio 3.0.32 github npm
@ai-sdk/gateway 4.0.104 github npm
@ai-sdk/gladia 3.0.54 github npm
@ai-sdk/gmicloud 3.0.34 github npm
@ai-sdk/google 4.0.88 github npm
@ai-sdk/google-vertex 5.0.102 github npm
@ai-sdk/groq 4.0.55 github npm
@ai-sdk/harness 1.0.139 github npm
@ai-sdk/harness-acp 1.0.78 github npm
@ai-sdk/harness-claude-code 1.0.143 github npm
@ai-sdk/harness-cline 1.0.66 github npm
@ai-sdk/harness-codex 1.0.141 github npm
@ai-sdk/harness-cursor 1.0.53 github npm
@ai-sdk/harness-deepagents 1.0.139 github npm
@ai-sdk/harness-fx 1.0.53 github npm
@ai-sdk/harness-github-copilot 1.0.35 github npm
@ai-sdk/harness-grok-build 1.0.77 github npm
@ai-sdk/harness-opencode 1.0.141 github npm
@ai-sdk/harness-pi 1.0.141 github npm
@ai-sdk/huggingface 2.0.63 github npm
@ai-sdk/hume 3.0.54 github npm
@ai-sdk/klingai 4.0.56 github npm
@ai-sdk/langchain 3.0.128 github npm
@ai-sdk/llamaindex 3.0.128 github npm
@ai-sdk/luma 3.0.55 github npm
@ai-sdk/mcp 2.0.67 github npm
@ai-sdk/minimax 3.0.49 github npm
@ai-sdk/mistral 4.0.57 github npm
@ai-sdk/moonshotai 3.0.63 github npm
@ai-sdk/open-responses 2.0.59 github npm
@ai-sdk/openai 4.0.84 github npm
@ai-sdk/openai-compatible 3.0.63 github npm
@ai-sdk/otel 1.0.128 github npm
@ai-sdk/perplexity 5.0.6 github npm
@ai-sdk/policy-opa 1.0.128 github npm
@ai-sdk/prodia 2.0.55 github npm
@ai-sdk/provider 4.0.22 github npm
@ai-sdk/provider-utils 5.0.54 github npm
@ai-sdk/quiverai 2.0.55 github npm
@ai-sdk/react 4.0.131 github npm
@ai-sdk/replicate 3.0.55 github npm
@ai-sdk/revai 3.0.54 github npm
@ai-sdk/rsc 3.0.128 github npm
@ai-sdk/sandbox-just-bash 1.0.139 github npm
@ai-sdk/sandbox-vercel 1.0.139 github npm
@ai-sdk/svelte 5.0.128 github npm
@ai-sdk/togetherai 3.0.64 github npm
@ai-sdk/topaz 3.0.1 github npm
@ai-sdk/tui 1.0.129 github npm
@ai-sdk/typesafe-ai 3.0.13 github npm
@ai-sdk/valibot 3.0.54 github npm
@ai-sdk/voyage 2.0.54 github npm
@ai-sdk/vue 4.0.128 github npm
@ai-sdk/workflow 2.0.59 github npm
@ai-sdk/workflow-harness 1.0.139 github npm
@ai-sdk/xai 5.0.15 github npm
@ai-sdk/zai 3.0.25 github npm

github-actions[bot] avatar Oct 05 '26 18:10 github-actions[bot]