Skip to content

Sentry — WebMCP Errors & Performance

One webmcp.configure for errors + performance. Hooks wrap only the agent path. See Analytics Overview and Step-by-Step.

For hook lifecycle, see Guide — Hooks. For types, see Reference — Hooks.

When to use

Sentry for error reporting and performance — captureException on error, startInactiveSpan on before/after, addBreadcrumb on denied.

External: Sentry Browser docs · @sentry/browser on npm

Step-by-step

Step 1 — Install & init Sentry

bash
npm i @sentry/browser
ts
import * as Sentry from '@sentry/browser';
Sentry.init({ dsn: 'https://...@o123.ingest.sentry.io/456', tracesSampleRate: 0.1 });

See Sentry — Install Browser JS.

Step 2 — Configure hooks once (global)

ts
import * as Sentry from '@sentry/browser';
import { webmcp } from 'simple-webmcp';

webmcp.configure({
  hooks: {
    before: [({tool, invocationId, metadata})=>{
      metadata.sentrySpan = Sentry.startInactiveSpan({ name: `webmcp:${tool.tool.name}`, op: 'webmcp.execute' });
      Sentry.addBreadcrumb({ category:'webmcp', message:`invoked ${tool.tool.name} #${invocationId.slice(0,8)}`, level:'info' });
    }],
    after: [({metadata})=>{
      (metadata.sentrySpan as any)?.end();
    }],
    error: [({tool, error, input})=>{
      Sentry.captureException(error, { tags:{ tool: tool.tool.name }, extra:{ input } });
      ((Sentry as any).getCurrentScope?.().getSpan?.() as any)?.end?.();
    }],
    denied: [({tool, reason})=>{
      Sentry.addBreadcrumb({ category:'webmcp', message:`denied ${tool.tool.name}: ${reason}`, level:'info' });
    }],
  }
});

error and denied are observational — throwing inside them is swallowed (see Hooks — Error).

Step 3 — Verify in the demo

  1. npm run docs:dev → open /demo.
  2. Inspect → Invoke checkout → approve → check Sentry Performance → webmcp:checkout span.
  3. Add a tool that throws → Invoke → check Sentry Issues → captureException with tool tag.
  4. Toggle Require approval for checkoutDeny → check Breadcrumbs → denied checkout.

Step 4 — Production notes

  • Sample rate: tracesSampleRate or tracesSampler for spans; avoid 100% in production.
  • PII: Don't attach raw input with email/token to extra — redact first (see Hooks — After).
  • Tenant: Enrich tags with tenantId via WebMCPProvider scoped before — see React.

Troubleshooting

SymptomFix
No span in PerformanceEnsure metadata.sentrySpan is set in before and .end() in after; check tracesSampleRate
Duplicate errorsDon't captureException in both error hook and fn — use hook only
Missing denieddenied runs only when before returns {action:'deny'} — see Hooks — Before

See also

Released under the MIT License.