Skip to content

fix(mcp): in-process tool errors return the masked message #5457

Done

Description

@dan-petty

Problem Description

When an in-process MCP tool call fails, the devops-cli MCP server returns only the class name of the exception. The agent cannot act on that answer.

Evidence:

  • src/devops_cli/ai/mcp/dispatcher.py, line 323: return 1, f"In-process execution error: {type(exc).__name__}". The message of the exception is not in the answer, and the debug log line holds only the class name too.
  • Claude Code transcripts, 2026-10-27 to 2026-11-13: 34 devops-cli MCP calls, 3 failures. The MCP server writes only uvicorn access lines, so no other record holds the cause.
  • The project rule says that output keeps the facts that the reader needs, such as error lines.

Reproduction Steps

  1. Call an in-process MCP tool with an argument that makes the command raise (for example, a path that does not exist).
  2. See the answer In-process execution error: <ExceptionClass> with no message.

Expected vs Actual Behavior

Expected: the answer holds the class and the first line of the message, passed through mask_secrets and cut to a fixed length. One log record at warning level holds the tool name, the class and the masked first line. Actual: the answer and the debug log hold only the class.

Proposed Solution

  • Return In-process execution error: <Class>: <first line>, with the first line passed through mask_secrets (src/devops_cli/security/sanitizer.py) and cut to 200 characters.
  • Log the same text at warning level with the tool name, so that Loki can count MCP tool errors.

Acceptance Criteria

  • A test (written first, fails on the base) makes the in-process dispatcher raise ValueError("bad path x") and proves that the answer holds ValueError: bad path x.
  • A test proves that a secret-like value in the message is masked, and that a long message is cut.
  • A test proves that one warning record names the tool and the class.
  • uv run devops ci fail-to-pass --base origin/main shows that the new tests fail on the base.
  • Measure in telemetry: the warning records of failed MCP tool calls appear in Loki, by tool and class.

Existing Tool Considered

mask_secrets, which the project already uses for error text. No new tool. Cost to build (estimate): 0.05 M model input and output, and 1 hour. Cost to keep: near 0.

Related

#4784 (bound the GitHub MCP tools).

Alternatives check of 2026-11-23: none.
Tool: none found.
Reason: Searches found only the MCP SDK and generic redaction or log tooling; none turns an in-process exception into a masked, length-bounded answer with a tool-labelled warning. The fix belongs in the dispatcher and reuses the project's own mask_secrets helper, as the issue says.

Proposed design

Problem statement

An in-process MCP tool call that raises returns only the exception class. The agent cannot see the cause. The change returns the class and the masked first line of the message. It writes a warning with the tool label and the class.

Acceptance criteria

Requirement: Return masked first line

The in-process dispatcher SHALL return the exception class and the masked first line of its message.

Scenario: ValueError message

  • WHEN A test makes the in-process dispatcher raise ValueError with message bad path x.
  • THEN The returned text is In-process execution error: ValueError: bad path x.

Requirement: Mask before cut

The in-process dispatcher MUST pass the first line through mask_secrets before it cuts the text to 200 characters.

Scenario: Secret at the cut

  • WHEN A test makes the dispatcher raise ValueError with 190 a characters followed by token=supersecretvalue123.
  • THEN The returned text contains token=<mas and does not contain token=supe or supersecretvalue123.

Requirement: Bound returned message

The in-process dispatcher SHALL cut the masked first line to 200 characters.

Scenario: Long message

  • WHEN A test makes the dispatcher raise ValueError with a message of 300 x characters.
  • THEN The returned text holds exactly 200 x characters after In-process execution error: ValueError: .

Requirement: Warning names tool and class

The in-process dispatcher SHALL write one warning record that names the tool label and the exception class, and SHALL NOT write the exception message.

Scenario: Warning for a failed tool

  • WHEN A test dispatches devops gh pr view and makes the in-process invocation raise ValueError with message bad path x.
  • THEN One record at warning level contains gh and ValueError, and it does not contain bad path x.

Requirement: Fail-to-pass check

The change SHALL include tests that fail on the base revision and pass on the head revision.

Scenario: Fail-to-pass command

  • WHEN A developer runs uv run devops ci fail-to-pass --base origin/main.
  • THEN The command reports that the new tests fail on the base revision and pass on the head revision.

Existing Tool Considered

The alternatives check of 2026-11-17 in the issue body records none; no external tool applies. The fix reuses the existing mask_secrets helper (src/devops_cli/security/sanitizer.py) for masking. Written cost: about 0.05 M model input and output tokens and 1 hour; cost to keep is near zero.

Key questions

  • Where does the in-process dispatcher turn an exception into the tool answer?: In InProcessDispatcher._dispatch_typer, the except Exception as exc block returns 1 and In-process execution error: {type(exc).__name__}. The same block logs only the class at debug level. [sources: src/devops_cli/ai/mcp/dispatcher.py]
  • How does the dispatcher form and bound the returned message?: Take the first line of str(exc), pass it through mask_secrets, cut it to 200 characters, and return In-process execution error: <Class>: <text>. When the first line is empty, return In-process execution error: <Class>. (best practice) [sources: src/devops_cli/ai/mcp/dispatcher.py, src/devops_cli/security/sanitizer.py:468]
    • Chosen: Mask then cut. A secret that crosses the cut stays masked.
    • Rejected: Cut then mask. A secret can cross the cut and leave an unmasked prefix.
    • Rejected: Return the full message. A long or multi-line message does not fit the MCP answer.
  • What does the warning record hold, given the CodeQL note in the dispatcher?: Log the tool label and the exception class at warning level. Do not log the exception message. The returned text carries the masked message to the agent. This keeps the Loki count and avoids the clear-text logging alerts that the dispatcher comment records. (best practice) [sources: src/devops_cli/ai/mcp/dispatcher.py]
    • Rejected: Log tool, class and masked message. The dispatcher comment records that masked values still raised CodeQL clear-text logging alerts and blocked v0.2.22.
    • Chosen: Log tool and class only. The warning still names the failed tool and class for Loki, and no caller data reaches the log.
    • Rejected: Log class only. Loki cannot count failures by tool.
  • How does the dispatcher name the tool in the warning?: Use the first devops subcommand from sub_args as the tool label, and use unknown when it is empty. The dispatcher has no separate MCP tool name. (best practice) [sources: src/devops_cli/ai/mcp/dispatcher.py]
    • Rejected: Add a tool_name parameter to dispatch. It changes the McpDispatcher protocol and every MCP tool caller for one log field.
    • Rejected: Use cmd[0]. cmd[0] is devops or uv, so it names no tool.
    • Chosen: Use the first devops subcommand. It is the stable label that the dispatcher already computes, and it needs no protocol change.
  • Which existing helper masks secrets in text?: mask_secrets in src/devops_cli/security/sanitizer.py masks known secret patterns and returns the text. [sources: src/devops_cli/security/sanitizer.py:468]

Fit

Fits in one pull request.

Suspected block

None.

Dependencies

None.

Activity

  1. added
    source/agent
    type/bug
    scope/mcp
    status/new
    value/medium
    effort/low
    on Nov 13, 2026
  2. dan-petty commented on Nov 13, 2026

    @dan-petty
    OwnerAuthor
  3. added
    priority/p2-medium
    on Nov 13, 2026
  4. devops-cli-service commented on Nov 17, 2026

    @devops-cli-service
    Contributor

    Alternatives check: none

    Reason: The issue asks for no new tool: the fix is a few lines in the project's own in-process MCP dispatcher (src/devops_cli/ai/mcp/dispatcher.py) that reuse the existing mask_secrets helper, add a warning log record, and cover it with tests. Searches for an MCP error-handling or secret-redaction tool/library that would supply this behaviour found nothing that fits the project's dispatcher, answer format and Loki logging, so no external tool or library applies.

    Tool: none found

    Lacks:

    • No external tool returns In-process execution error: <Class>: <first line> from the project's in-process dispatcher
    • No external tool masks the message with the project's own mask_secrets (src/devops_cli/security/sanitizer.py)
    • No external tool writes the single warning record with the tool name and exception class that Loki counts
    • No external tool supplies the fail-to-pass tests the acceptance criteria require

    Searches run:

    • python library redact secrets from exception message before logging, results: 5
    • MCP server tool call error return exception message instead of class name, results: 5
    • FastMCP tool error message secret masking built in, results: 5

    Checked on 2026-11-17. A person decides what this means for the item. The check changes no label and closes no issue.

  5. added
    status/ready
    and removed
    status/new
    on Nov 18, 2026
  6. devops-cli-service commented on Nov 18, 2026

    @devops-cli-service
    Contributor

    Status set to Ready at b466159 (see proposed design in issue body).

    Moved to new items: #6237.

  7. added
    owner/agent
    status/in-progress
    status/in-review
    and removed
    status/ready
    status/in-progress
    on Nov 29, 2026
  8. removed
    status/in-review
    owner/agent
    on Nov 29, 2026
  9. devops-cli-service commented on Nov 29, 2026

    @devops-cli-service
    Contributor

    Live since 2026-11-29 16:17 UTC

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    effort/lowpriority/p2-mediumscope/mcpsource/agenttype/bugvalue/medium

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions