Skip to content

fix(mcp): return structured error responses when tool handlers throw#11

Draft
Copilot wants to merge 2 commits intomainfrom
copilot/check-code-runnability
Draft

fix(mcp): return structured error responses when tool handlers throw#11
Copilot wants to merge 2 commits intomainfrom
copilot/check-code-runnability

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 26, 2026

When tool handlers threw (e.g., RuntimePreflightError from a failed preflight like missing Shopify CLI), the MCP SDK's internal createToolError was called — which returns { isError: true } with no structuredContent. The stdio smoke test expects structuredContent.error to always be defined when isError is true, causing a TypeError crash.

Summary

  • Added toToolError(error) helper in server.ts that converts any thrown exception into a proper structured result:
    { content, structuredContent: { error: { code: "tool_execution_failed", message } }, isError: true }
  • Wrapped the four throwable tool callbacks (replicate_site_to_theme, replicate_site_to_hydrogen, get_replication_job, list_replication_jobs) in try/catch using this helper — list_destination_stores is synchronous and not at risk
  • list_destination_stores is unaffected (no preflight, no async throws)
  • Updated a misleading test description in server.test.ts: "returns a structured runtime preflight error when replication cannot start" → "re-throws runtime preflight errors from the orchestrator without wrapping" — the test exercises the handler layer, not the server wrapper

Verification

  • pnpm build
  • pnpm test
  • pnpm typecheck
  • pnpm theme:check

Risk

No API or contract changes. Error structure (structuredContent.error.code) is new surface area on the failure path only — previously clients received undefined structured content on throw, which was a bug.

Original prompt

Run a full check to see if the code is actually runnable. Also explore possible issues that this mcp could have.


💬 Send tasks to Copilot coding agent from Slack and Teams to turn conversations into code. Copilot posts an update in your thread when it's finished.

Copilot AI changed the title [WIP] Run full check for code runnable status and explore issues fix(mcp): return structured error responses when tool handlers throw Mar 26, 2026
Copilot AI requested a review from connortessaro March 26, 2026 14:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants