Skip to content

feat(http): add opt-in Sentry error reporting - #243

Merged
Gerardo Sabetta (gerardosabetta) merged 4 commits into
mainfrom
gerardosabetta/http-sentry
Nov 25, 2026
Merged

Gerardo Sabetta (gerardosabetta) merged 4 commits into
mainfrom
gerardosabetta/http-sentry

Conversation

@gerardosabetta

@gerardosabetta Gerardo Sabetta (gerardosabetta) commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

HTTP mode can now report server-side failures to Sentry. It is off unless SOCKET_MCP_SENTRY_DSN is set, and stdio mode never reports, even with the DSN set. Without the DSN the Sentry SDK is never evaluated and nothing is sent, so local and Claude Desktop users see no change.

What changed

  • Env vars. SOCKET_MCP_SENTRY_DSN turns reporting on in HTTP mode. SOCKET_MCP_SENTRY_ENVIRONMENT names the environment; Sentry's default is production. Both are in the HTTP env table in docs/commands.md (which the README links to), in a new docs/service.md#error-reporting section and in a short README section.
  • SDK. @sentry/core 11.13.0 with a small transport on the repo's shared httpRequest helper (Node's own HTTP and HTTPS modules): no OpenTelemetry, no module patching, no tracing. The DSN must use HTTPS; cleartext HTTP is accepted only for a loopback recorder. It's loaded by a dynamic import only when reporting starts, and rolldown keeps it behind a lazy initializer in dist/index.cjs. The package still has no runtime dependencies.
  • Events. A tool handler throws. An upstream 5xx, network error, timeout, unparseable response or other caught exception behind a tool. An anonymous call whose fallback to the deploy token is rejected with 401 or 403. MCP handler and adapter onerror, routing, body reads and OAuth token verification.
  • Breadcrumb only. Invalid arguments, unknown tools, a caller's missing or rejected token, other upstream 4xx answers, requests the MCP handler rejects for the client, and client disconnects.
  • Tags. mcp.tool, mcp.client, mcp.client_version, auth.outcome, tool.outcome, failure.cause, upstream.status, error.code, error.source and transport, all from bounded vocabularies; release is the package version. Tool-call events are grouped by tool, outcome, auth outcome and cause, and server errors by source and error type, never by message text.
  • Privacy. beforeSend rebuilds every event from an allowlist. The exception message becomes a summary built from the tags, because upstream errors embed request URLs and bodies. Stack frames keep the function, file basename, line and column. Tokens, the audit token hash, request headers (Authorization, Cookie, Mcp-Session-Id), query strings, tool arguments and results, users and the host name are dropped. Sentry v11 removed sendDefaultPii; its replacement, dataCollection, is switched fully off. A malformed DSN, or one using HTTP off loopback, is rejected before the SDK sees it, because the SDK would print it.
  • Shutdown. SIGINT and SIGTERM wait up to 2 s for queued events, then exit. Each send is tried once, follows no redirects and gives up after 5 s, so an unreachable Sentry never blocks a request.
  • Supporting refactor (first commit). markUpstreamHttpOutcome now records why an upstream call failed next to the existing denied mark. depscore marks its connection, status and parse failures the same way (an NDJSON body with no parseable lines counts; one of only purlError/summary frames, meaning every purl was unresolved, does not), and the blob and file-list fetches keep the original error as cause. Audit outcomes are unchanged.

Client identity

2026-07-28 requests carry clientInfo in the per-request _meta envelope (ctx.mcpReq.envelope), so mcp.client uses clientInfo.name. 2025-era clients on the stateless legacy leg send clientInfo only on initialize, which is its own exchange, so the per-request Server never sees it. Those calls fall back to the User-Agent's first product token, else unknown.

Reviewer heads-up

  • Two causes beyond the original scope are reported: other exceptions a tool caught (a bad upstream body, a bug), and operator_denied, where an anonymous call's fallback to the deploy's own token is rejected. That is the deploy's credential failing, not the caller's.
  • The fingerprint adds the failure cause to tool, outcome and auth outcome, so timeouts and a handler bug on one tool don't share an issue.
  • The MCP handler's onerror also receives client rejections. They are matched by error class and by message prefix (Rejected …, Unsupported Media Type), which an SDK upgrade could change.
  • No version bump or publish. The hosted deployment will set SOCKET_MCP_SENTRY_DSN for this package after a release.

Cost

Measure main This PR
dist/index.cjs 3,066,463 B 3,227,677 B (+5.3%)
dist/index.cjs, gzip -9 595,870 B 635,469 B (+6.6%)
HTTP start to listening, no DSN 76.8 ms 77.4 ms
HTTP start to listening, DSN set – 78.7 ms
socket-mcp --help 76.5 ms 78.3 ms
.mcpb bundle 967.8 kB 1019.6 kB

Startup figures are medians of 60 interleaved runs on one machine (Node 26.12.1), measured before the review changes; the bundle sizes are re-measured after them. The slides below predate the review changes, so they still show the fetch transport and the earlier byte counts. For the SDK choice, each candidate was bundled alone with the repo's rolldown and timed for load and init over bare node: @sentry/core with a fetch transport (since replaced by httpRequest, which the bundle already includes) is 194 KB and +3.2 ms; @sentry/node is 1.35 MB and +16.9 ms without default integrations, +32.9 ms with them; @sentry/node-core 10.x is 943 KB and fails to load without its OpenTelemetry peers.

Testing

  • pnpm test: 74 files, 684 tests pass, 30 of them new. Unit tests cover the off path (no DSN, or stdio, never loads the Sentry module or sends a request), tag derivation, the event-or-breadcrumb decision and the allowlist sanitizer. Integration tests drive the real HTTP stack wired as startHttpServer wires it, with nock for the Socket API and a recording transport. Each event class sends exactly one event with the expected tags; invalid, unknown, denied and 4xx calls send none; no token, token hash, org slug, cursor, cookie, upstream body, query key or home path reaches the envelope; an unreachable DSN leaves tool calls working.
  • pnpm run test:e2e: 3 files, 19 tests pass, 3 of them new. The built bin in HTTP mode, with the DSN pointed at a local recorder and SOCKET_API_BASE_URL at a local server that answers 500, sends one scrubbed event and exits promptly on SIGTERM. Without a DSN, or in stdio mode with one, it sends nothing.
  • pnpm run fmt, pnpm run lint, pnpm run repo:check and pnpm run check pass. mcpb validate and mcpb pack pass.
  • Not covered: delivery to a real Sentry project; build-mcpb's npm install --production step, which needs npm 12.2 or later and fails the same way on main locally with npm 11.20; a live OAuth issuer outage, which is exercised through a direct call; flushing an in-flight event on SIGTERM.
Sample event from the built bin (local recorder, synthetic values)
{
  "contexts": {
    "runtime": {
      "name": "node",
      "version": "v26.12.1"
    }
  },
  "exception": {
    "values": [
      {
        "type": "UpstreamHttpError",
        "value": "alerts upstream_5xx 502",
        "mechanism": {
          "type": "socket-mcp",
          "handled": true
        },
        "stacktrace": {
          "frames": [
            {
              "function": "Object.handler",
              "filename": "index.cjs",
              "lineno": 43717,
              "colno": 18,
              "in_app": true
            },
            {
              "function": "process.processTicksAndRejections",
              "filename": "node:internal/process/task_queues",
              "lineno": 104,
              "colno": 5,
              "in_app": false
            },
            {
              "function": "fetchAlerts",
              "filename": "index.cjs",
              "lineno": 42030,
              "colno": 21,
              "in_app": true
            }
          ]
        }
      }
    ]
  },
  "tags": {
    "auth.outcome": "bearer_ok",
    "mcp.client": "example-client",
    "mcp.client_version": "1.4.0",
    "mcp.tool": "alerts",
    "tool.outcome": "failure",
    "transport": "http",
    "failure.cause": "upstream_5xx",
    "upstream.status": "502"
  },
  "event_id": "7d5448859dab4b3fb9e73255437a0d8a",
  "timestamp": 1795543726.195,
  "level": "error",
  "platform": "node",
  "release": "0.4.0",
  "environment": "example",
  "fingerprint": [
    "socket-mcp",
    "tool",
    "alerts",
    "failure",
    "bearer_ok",
    "upstream_5xx"
  ]
}

Slides

cover

gap

evidence

flow

scope

tags

privacy

sdk

cost

headsup

verify

🤖 Generated with Claude Code

Tool results already carry a denied or invalid mark for the audit
outcome. Record the failure cause beside it (upstream 4xx or 5xx, a
denial, a network error, a timeout, or another exception) together with
the error the tool caught, so a later consumer can tell a server-side
failure from the caller's mistake without reading message text.

depscore now marks its connection, status and parse failures the same
way the other tools do, and the blob and file-list fetches keep the
original request error as `cause`, so its code survives the rewrap.
HTTP mode can now report server-side failures to Sentry. It is off
unless SOCKET_MCP_SENTRY_DSN is set; SOCKET_MCP_SENTRY_ENVIRONMENT names
the environment. stdio mode never reports, even with the DSN set, and
without the DSN the Sentry SDK is never loaded.

The client is built from @sentry/core alone, with a small fetch
transport: no OpenTelemetry, no module patching, no tracing. It is
loaded by a dynamic import only when reporting is on, which the bundle
keeps behind a lazy initializer.

Events cover a tool handler throwing, upstream 5xx, network, timeout and
other unexpected failures behind a tool, a rejected deploy token on an
anonymous call, and serving, routing, body-read and token-verification
failures. Invalid input, unknown tools, a caller's denied token,
upstream 4xx answers, client rejections and client disconnects leave a
breadcrumb instead.

Each event carries bounded tags (mcp.tool, mcp.client and its version,
auth.outcome, tool.outcome, failure.cause, upstream.status, error.code,
error.source, transport) and the package version as its release. It is
grouped by tool, outcome, auth outcome and cause rather than message
text. beforeSend rebuilds every event from an allowlist: messages become
a summary built from the tags, stack frames keep only function, file
basename and position, and request data, headers, tokens, arguments,
query strings, users and the host name are dropped. SIGINT and SIGTERM
wait up to two seconds for queued events before exiting.
@socket-security

socket-security Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Review the following changes in direct dependencies. Learn more about Socket for GitHub.

Diff Package Supply Chain
Security
Vulnerability Quality Maintenance License
Addednpm/​@​sentry/​core@​11.13.0991008496100

View full report

Gerardo Sabetta (gerardosabetta) added a commit that referenced this pull request Nov 24, 2026
@gerardosabetta
Gerardo Sabetta (gerardosabetta) marked this pull request as ready for review November 24, 2026 21:18
@cursor

cursor Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

PR Summary

Medium Risk
Adds optional third-party error telemetry in HTTP mode with heavy privacy scrubbing, but misconfiguration could still leak data if sanitization regresses; request-path reporting hooks touch core serving and tool flows.

Overview
Adds opt-in Sentry error reporting for HTTP mode when SOCKET_MCP_SENTRY_DSN is set (SOCKET_MCP_SENTRY_ENVIRONMENT labels events). stdio ignores reporting; without a DSN the Sentry SDK is never loaded.

Reporting is wired through new error-reporting modules: lazy @sentry/core init at HTTP startup, allowlist-based event scrubbing (tag summaries instead of raw messages), bounded tags/fingerprints, breadcrumbs for caller-side failures, SIGINT/SIGTERM flush, and non-blocking sends with timeouts. The HTTP stack, tool dispatch, and OAuth verification call into reportServerError / reportToolCall; client rejections and disconnects are mostly breadcrumbs only.

Tool upstream handling is extended so failures carry classified causes (markUpstreamHttpOutcome, error cause chains on blob/file fetches), enabling accurate Sentry tags. Docs and env tables document the new variables; unit, integration, and e2e tests cover scrubbing and event selection.

Reviewed by Cursor Bugbot for commit 9389357. Configure here.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit 9389357. Configure here.

Comment thread lib/tool-depscore.mts Resolved

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

One transport implementation detail to adjust.

Comment thread lib/error-reporting-sentry.mts Resolved

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

fetch use switcheroo, 🙏

When every NDJSON line from the depscore endpoint fails to parse, the
tool returned an unmarked error result, so error reporting treated it as
a caller-side outcome and sent nothing. Mark that case as a failure the
server could not handle. A body made only of control frames (every purl
unresolved) stays unmarked: that is the caller's outcome.
Use the shared httpRequest helper from @socketsecurity/lib, which uses
Node's own HTTP and HTTPS modules, instead of the global fetch. Send
each envelope once, with no redirects and a five-second timeout.

Require an HTTPS DSN. Cleartext HTTP is accepted only for a loopback
host, for local test recorders. The loopback check moves from the
browser sign-in module into lib/http.mts so both share it.
@socket-security-staging

Copy link
Copy Markdown

Review the following changes in direct dependencies. Learn more about Socket for GitHub.

Diff Package Supply Chain
Security
Vulnerability Quality Maintenance License
Addednpm/​@​sentry/​core@​11.13.01001008496100

View full report

@gerardosabetta
Gerardo Sabetta (gerardosabetta) merged commit e19e21f into main Nov 25, 2026
12 checks passed
@gerardosabetta
Gerardo Sabetta (gerardosabetta) deleted the gerardosabetta/http-sentry branch November 25, 2026 16:27
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