Overview
Every HTTP request to your FrontMCP server creates aFrontMcpContext that:
- Propagates through all stages, tools, resources, and prompts via AsyncLocalStorage
- 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 thecontext getter for convenient access:
Safe Access
UsetryGetContext() 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 afetch() method that automatically injects headers:
Configuration
Configure fetch behavior via context config: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
| Header | Description |
|---|---|
traceparent | W3C standard: {version}-{trace-id}-{parent-id}-{flags} |
tracestate | Vendor-specific trace data |
x-frontmcp-trace-id | Fallback for non-W3C clients |
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:Logging
Get a child logger with context attached:API Reference
FrontMcpContext Class
| Property | Type | Description |
|---|---|---|
requestId | string | Unique request identifier (UUID v4) |
traceContext | TraceContext | W3C Trace Context data |
sessionId | string | MCP session identifier |
authInfo | Partial<AuthInfo> | Authentication information |
scopeId | string | Current scope identifier |
timestamp | number | Request start timestamp (ms since epoch) |
metadata | RequestMetadata | HTTP request metadata |
config | FrontMcpContextConfig | Context configuration |
transport | TransportAccessor | undefined | Transport for elicit requests |
flow | FlowBaseRef | undefined | Current flow reference |
scope | ScopeRef | undefined | Current scope reference |
sessionMetadata | SessionIdPayload | undefined | Session metadata (protocol, platform) |
| Method | Signature | Description |
|---|---|---|
mark | mark(name: string): void | Record a timing mark |
elapsed | elapsed(from?: string, to?: string): number | Get elapsed time in ms |
getMarks | getMarks(): ReadonlyMap<string, number> | Get all timing marks |
set | set<T>(key: string | symbol, value: T): void | Store context-scoped data |
get | get<T>(key: string | symbol): T | undefined | Retrieve context-scoped data |
has | has(key: string | symbol): boolean | Check if key exists |
delete | delete(key: string | symbol): boolean | Delete a key |
getLogger | getLogger(parent: FrontMcpLogger): FrontMcpLogger | Get child logger with context |
toLogContext | toLogContext(): Record<string, unknown> | Get summary for logging |
fetch | fetch(input: RequestInfo | URL, init?: RequestInit): Promise<Response> | Context-aware fetch |
updateAuthInfo | updateAuthInfo(authInfo: Partial<AuthInfo>): void | Update auth info (internal) |
updateSessionMetadata | updateSessionMetadata(metadata: SessionIdPayload): void | Update session metadata (internal) |
TraceContext Interface
| Property | Type | Description |
|---|---|---|
traceId | string | 32-character hex trace identifier |
parentId | string | undefined | 16-character hex parent span ID |
traceFlags | number | Trace flags (e.g., 1 for sampled) |
traceState | string | undefined | Vendor-specific trace state |
raw | string | Raw traceparent header value |
RequestMetadata Interface
| Property | Type | Description |
|---|---|---|
userAgent | string | undefined | User-Agent header |
contentType | string | undefined | Content-Type header |
accept | string | undefined | Accept header |
clientIp | string | undefined | Client IP address |
customHeaders | Record<string, string> | Headers matching x-frontmcp-* |
TransportAccessor Interface
| Property/Method | Type | Description |
|---|---|---|
supportsElicit | boolean | Whether transport supports elicit |
elicit | <T>(message: string, schema: T): Promise<ElicitResult> | Send elicit request |
Key Features
- Unified Context: All request-scoped data in one place
- Better Tracing: Access trace IDs alongside auth info
- Context-Aware Fetch: Auto-inject headers in outgoing requests
- Transport Access: Use elicit directly from context
- Timing Integration: Correlate auth with performance metrics
- Future-Proof: New features will be added to FrontMcpContext