> ## Documentation Index
> Fetch the complete documentation index at: https://docs.agentfront.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# ExecutionContextBase

> ExecutionContextBase is the abstract base class that provides common functionality for all entry context classes (ToolContext, ResourceContext, AgentContext, SkillContext).

## Class Definition

```typescript theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
export abstract class ExecutionContextBase<Out = unknown> {
  readonly runId: string;
  activeStage: string;
  protected readonly logger: FrontMcpLogger;
  protected readonly providers: ProviderRegistryInterface;
}
```

## Constructor

```typescript theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
type ExecutionContextBaseArgs = {
  providers: ProviderRegistryInterface;
  logger: FrontMcpLogger;
  authInfo: Partial<AuthInfo>;
};
```

## Properties

| Property      | Type             | Description                                 |
| ------------- | ---------------- | ------------------------------------------- |
| `runId`       | `string`         | Unique identifier for this execution (UUID) |
| `activeStage` | `string`         | Current execution stage (default: 'init')   |
| `logger`      | `FrontMcpLogger` | Logger instance with scope prefix           |

## Methods

### Dependency Injection

#### get\<T>(token)

Get a required dependency from the provider registry.

```typescript theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
get<T>(token: Token<T>): T
```

**Throws:** `DependencyNotFoundError` if token is not registered.

```typescript theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
const userService = this.get(UserServiceToken);
const config = this.get(ConfigService);
```

#### tryGet\<T>(token)

Try to get an optional dependency.

```typescript theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
tryGet<T>(token: Token<T>): T | undefined
```

```typescript theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
const cache = this.tryGet(CacheServiceToken);
if (cache) {
  const cached = await cache.get(key);
}
```

### Context Access

#### get context()

Get the current request context.

```typescript theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
get context(): FrontMcpContext
```

**Throws:** `RequestContextNotAvailableError` if not in request scope.

```typescript theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
const ctx = this.context;
const requestId = ctx.requestId;
const sessionId = ctx.sessionId;
```

#### tryGetContext()

Try to get request context without throwing.

```typescript theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
tryGetContext(): FrontMcpContext | undefined
```

```typescript theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
const ctx = this.tryGetContext();
if (ctx) {
  // We're in a request scope
}
```

### Scope Access

#### get scope

Get the current scope with all registries.

```typescript theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
get scope(): ScopeEntry
```

```typescript theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
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.

```typescript theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
getAuthInfo(): Partial<AuthInfo>
```

```typescript theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
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.

```typescript theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
mark(stage: string): void
```

```typescript theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
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.

```typescript theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
protected fail(err: Error): never
```

```typescript theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
if (!input.valid) {
  this.fail(new InvalidInputError('Input validation failed'));
}
```

#### get error

Get the current error, if any.

```typescript theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
protected get error(): Error | undefined
```

### HTTP Requests

#### fetch(input, init?)

Make HTTP requests with auto-injected headers.

```typescript theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
fetch(input: RequestInfo | URL, init?: RequestInit): Promise<Response>
```

Automatically injects:

* Authorization headers
* Trace context headers
* Custom headers from context

```typescript theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
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).

```typescript theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
get config(): ConfigService
```

```typescript theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
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.

```typescript theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
protected get contextLogger(): FrontMcpLogger
```

```typescript theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
this.contextLogger.info('Processing request', {
  input,
  requestId: this.context.requestId,
});
```

## Usage Example

```typescript theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
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;
  }
}
```

## Related

<CardGroup cols={2}>
  <Card title="ToolContext" icon="wrench" href="/frontmcp/sdk-reference/contexts/tool-context">
    Tool-specific context
  </Card>

  <Card title="ResourceContext" icon="file" href="/frontmcp/sdk-reference/contexts/resource-context">
    Resource-specific context
  </Card>

  <Card title="AgentContext" icon="robot" href="/frontmcp/sdk-reference/contexts/agent-context">
    Agent-specific context
  </Card>

  <Card title="ProviderRegistry" icon="database" href="/frontmcp/sdk-reference/registries/provider-registry">
    Dependency injection
  </Card>
</CardGroup>
