Skip to main content

Class Definition

export abstract class ExecutionContextBase<Out = unknown> {
  readonly runId: string;
  activeStage: string;
  protected readonly logger: FrontMcpLogger;
  protected readonly providers: ProviderRegistryInterface;
}

Constructor

type ExecutionContextBaseArgs = {
  providers: ProviderRegistryInterface;
  logger: FrontMcpLogger;
  authInfo: Partial<AuthInfo>;
};

Properties

PropertyTypeDescription
runIdstringUnique identifier for this execution (UUID)
activeStagestringCurrent execution stage (default: ‘init’)
loggerFrontMcpLoggerLogger instance with scope prefix

Methods

Dependency Injection

get<T>(token)

Get a required dependency from the provider registry.
get<T>(token: Token<T>): T
Throws: DependencyNotFoundError if token is not registered.
const userService = this.get(UserServiceToken);
const config = this.get(ConfigService);

tryGet<T>(token)

Try to get an optional dependency.
tryGet<T>(token: Token<T>): T | undefined
const cache = this.tryGet(CacheServiceToken);
if (cache) {
  const cached = await cache.get(key);
}

Context Access

get context()

Get the current request context.
get context(): FrontMcpContext
Throws: RequestContextNotAvailableError if not in request scope.
const ctx = this.context;
const requestId = ctx.requestId;
const sessionId = ctx.sessionId;

tryGetContext()

Try to get request context without throwing.
tryGetContext(): FrontMcpContext | undefined
const ctx = this.tryGetContext();
if (ctx) {
  // We're in a request scope
}

Scope Access

get scope

Get the current scope with all registries.
get scope(): ScopeEntry
const tools = this.scope.tools;
const resources = this.scope.resources;
const prompts = this.scope.prompts;
const agents = this.scope.agents;
const skills = this.scope.skills;

Authentication

getAuthInfo()

Get authentication information for the current request.
getAuthInfo(): Partial<AuthInfo>
const auth = this.getAuthInfo();
const user = auth.user;
const token = auth.accessToken;
const scopes = auth.scopes;

Execution Tracking

mark(stage)

Mark the current execution stage for debugging and profiling.
mark(stage: string): void
this.mark('validation');
await validate(input);

this.mark('processing');
const result = await process(input);

this.mark('complete');

Error Handling

fail(err)

Fail the execution with an error.
protected fail(err: Error): never
if (!input.valid) {
  this.fail(new InvalidInputError('Input validation failed'));
}

get error

Get the current error, if any.
protected get error(): Error | undefined

HTTP Requests

fetch(input, init?)

Make HTTP requests with auto-injected headers.
fetch(input: RequestInfo | URL, init?: RequestInit): Promise<Response>
Automatically injects:
  • Authorization headers
  • Trace context headers
  • Custom headers from context
const response = await this.fetch('https://api.example.com/data', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({ key: 'value' }),
});

if (!response.ok) {
  this.fail(new Error(`API error: ${response.status}`));
}

return response.json();

Configuration

get config

Get typed configuration access (requires ConfigPlugin).
get config(): ConfigService
const apiKey = this.config.get('API_KEY');
const port = this.config.getNumber('PORT', 3000);
const debug = this.config.getBoolean('DEBUG', false);

Protected Logger

get contextLogger

Get a child logger with request context.
protected get contextLogger(): FrontMcpLogger
this.contextLogger.info('Processing request', {
  input,
  requestId: this.context.requestId,
});

Usage Example

import { Tool, ToolContext } from '@frontmcp/sdk';
import { z } from 'zod';

@Tool({
  name: 'process_data',
  inputSchema: { data: z.string() },
})
class ProcessDataTool extends ToolContext {
  async execute(input: { data: string }) {
    // Unique run ID for this execution
    this.logger.info('Starting execution', { runId: this.runId });

    // Mark stages
    this.mark('validation');
    if (!input.data) {
      this.fail(new InvalidInputError('Data is required'));
    }

    // Get dependencies
    this.mark('setup');
    const processor = this.get(ProcessorServiceToken);
    const cache = this.tryGet(CacheServiceToken);

    // Check cache
    if (cache) {
      const cached = await cache.get(input.data);
      if (cached) {
        return cached;
      }
    }

    // Process
    this.mark('processing');
    const result = await processor.process(input.data);

    // Make external call
    this.mark('external-api');
    const enriched = await this.fetch('https://api.example.com/enrich', {
      method: 'POST',
      body: JSON.stringify(result),
    }).then(r => r.json());

    // Cache result
    if (cache) {
      await cache.set(input.data, enriched);
    }

    this.mark('complete');
    return enriched;
  }
}

ToolContext

Tool-specific context

ResourceContext

Resource-specific context

AgentContext

Agent-specific context

ProviderRegistry

Dependency injection