Skip to main content
FrontMcpContext provides per-request state that flows through the entire async execution chain. It enables distributed tracing, timing, request-scoped data storage, context-aware fetch, and transport access.

Overview

Every HTTP request to your FrontMCP server creates a FrontMcpContext that:
  • Propagates through all stages, tools, resources, and prompts via AsyncLocalStorage (in a browser without AsyncContext, requests run one at a time instead; see Browser Compatibility)
  • Provides W3C Trace Context for distributed tracing
  • Stores authentication information after verification
  • Tracks timing marks for performance monitoring
  • Provides request-scoped key-value storage
  • Offers context-aware fetch with auto-injection
  • Enables transport access for elicit requests

Accessing Context

From Tools/Resources/Prompts

Use the context getter for convenient access:

Safe Access

Use tryGetContext() when context may not be available (e.g., during initialization or in non-HTTP flows):

From CONTEXT-Scoped Providers

CONTEXT-scoped providers can receive FrontMcpContext via factory injection:

Context-Aware Fetch

FrontMcpContext provides a fetch() method that adds request context to outgoing requests:
The MCP authorization spec forbids passing the token a client sent to your server on to other services. By default fetch() sends neither the caller’s token nor its x-frontmcp-* headers anywhere. To call an upstream API with its own credentials, use credentials: { provider }.

Configuration

Configure fetch behavior for the whole server with @FrontMcp({ fetch }):
Upgrading from autoInjectAuthHeaders: that context option is gone. It sent the caller’s token to every origin. That token was issued for this MCP server, and the MCP authorization spec forbids passing it through to any other API. Call upstream APIs with credentials: { provider }, or with a token issued for that API, for example through token exchange. fetch.forwardCallerTokenTo only controls what the SDK sends. Listing an origin there does not make passing the token through compliant. Only list endpoints that belong to this same MCP server and validate the token as issued for it.
Entries are matched by origin (scheme, host and port), so https://api.internal.example/v1 allows every path on https://api.internal.example but not http://api.internal.example or https://other.api.internal.example. A request that carries the caller’s token or x-frontmcp-* headers never follows a redirect, so those headers never reach an origin you did not list. It is sent with redirect: 'manual', and a 3xx comes back to your code; if you ask for redirect: 'error', the redirect rejects the fetch instead. requestTimeout applies when you pass no signal in the options, and so does the Request’s own signal; a signal passed in the options replaces both. Inside a tool, this.fetch() is also aborted when the call is cancelled or its execution timeout passes.

Custom Headers

Additional headers can be passed and will be merged:

Transport Access (Elicit)

Access the transport for interactive prompts:

Distributed Tracing

FrontMcpContext automatically parses W3C Trace Context headers for distributed tracing compatibility.

Supported Headers

Using Trace Context

Integration with Observability Tools

FrontMCP’s trace context is compatible with:
  • OpenTelemetry
  • Datadog APM
  • AWS X-Ray
  • Jaeger
  • Zipkin

Timing & Performance

Track execution timing with marks for performance monitoring:

Performance Logging


Context-Scoped Storage

Store and retrieve data that lives only for the duration of the request:

Request Metadata

Access HTTP request metadata:

Authentication

Access authentication information via the context:
authInfo is Partial<AuthInfo> because authentication information is progressively populated during the request lifecycle. Fields are fully populated after the authorization stage completes.

Logging

Get a child logger with context attached:

API Reference

FrontMcpContext Class

TraceContext Interface

RequestMetadata Interface

TransportAccessor Interface


Key Features

  1. Unified Context: All request-scoped data in one place
  2. Better Tracing: Access trace IDs alongside auth info
  3. Context-Aware Fetch: Auto-inject headers in outgoing requests
  4. Transport Access: Use elicit directly from context
  5. Timing Integration: Correlate auth with performance metrics
  6. Future-Proof: New features will be added to FrontMcpContext