Skip to main content
PromptContext does not extend ExecutionContextBase but provides a similar interface for consistency.

Class Definition

export abstract class PromptContext {
  readonly args: Record<string, string>;
  readonly metadata: PromptMetadata;
  readonly runId: string;
}

Properties

PropertyTypeDescription
metadataPromptMetadataPrompt metadata (name, description, etc.)
argsRecord<string, string>Arguments passed to the prompt
outputGetPromptResult | undefinedPrompt result (after execution)
promptNamestringPrompt name
promptIdstringPrompt ID
authInfoAuthInfoAuthentication information
runIdstringUnique execution identifier

Abstract Method

execute(args)

The main execution method that must be implemented.
abstract execute(args: Record<string, string>): Promise<GetPromptResult>

Return Format

Prompts must return GetPromptResult:
interface GetPromptResult {
  description?: string;
  messages: Array<{
    role: 'user' | 'assistant';
    content: TextContent | ImageContent | EmbeddedResource;
  }>;
}

Methods

Dependency Injection

get<T>(token)

Get a required dependency.
get<T>(token: Token<T>): T
const templateEngine = this.get(TemplateEngineToken);

tryGet<T>(token)

Try to get an optional dependency.
tryGet<T>(token: Token<T>): T | undefined

Response Methods

respond(value)

Set output and end execution immediately.
respond(value: GetPromptResult): never

Scope Access

scope

Get the current scope with all registries.
get scope(): ScopeEntry

Execution Tracking

mark(stage)

Mark current execution stage.
mark(stage: string): void

Error Handling

fail(err)

Fail execution with an error.
protected fail(err: Error): never

History

outputHistory

History of output changes.
get outputHistory(): ReadonlyArray<GetPromptResult>

Content Types

Text Content

{
  role: 'user',
  content: {
    type: 'text',
    text: 'Your prompt text here',
  },
}

Image Content

{
  role: 'user',
  content: {
    type: 'image',
    data: 'base64-encoded-image-data',
    mimeType: 'image/png',
  },
}

Embedded Resource

{
  role: 'user',
  content: {
    type: 'resource',
    resource: {
      uri: 'file://context.md',
      text: 'Resource content here',
      mimeType: 'text/markdown',
    },
  },
}

Basic Example

import { Prompt, PromptContext } from '@frontmcp/sdk';

@Prompt({
  name: 'greeting',
  description: 'Generate a personalized greeting',
  arguments: [
    { name: 'name', description: 'Name to greet', required: true },
    { name: 'style', description: 'Greeting style', required: false },
  ],
})
class GreetingPrompt extends PromptContext {
  async execute(args: Record<string, string>) {
    const style = args.style || 'friendly';

    return {
      description: `Greeting for ${args.name}`,
      messages: [
        {
          role: 'user',
          content: {
            type: 'text',
            text: `Generate a ${style} greeting for ${args.name}.`,
          },
        },
      ],
    };
  }
}

Multi-Turn Conversation

import { Prompt, PromptContext } from '@frontmcp/sdk';

@Prompt({
  name: 'code-review',
  description: 'Code review conversation',
  arguments: [
    { name: 'code', required: true },
    { name: 'language', required: true },
  ],
})
class CodeReviewPrompt extends PromptContext {
  async execute(args: Record<string, string>) {
    return {
      description: 'Code review conversation',
      messages: [
        {
          role: 'user',
          content: {
            type: 'text',
            text: `You are an expert ${args.language} code reviewer. Focus on:
- Code quality and best practices
- Potential bugs and edge cases
- Performance considerations
- Security issues`,
          },
        },
        {
          role: 'assistant',
          content: {
            type: 'text',
            text: `I'll review your ${args.language} code focusing on quality, bugs, performance, and security. Please share the code.`,
          },
        },
        {
          role: 'user',
          content: {
            type: 'text',
            text: `Please review this code:\n\n\`\`\`${args.language}\n${args.code}\n\`\`\``,
          },
        },
      ],
    };
  }
}

With Template Engine

import { Prompt, PromptContext, Provider, Token } from '@frontmcp/sdk';

export const TemplateEngineToken = new Token<TemplateEngine>('TemplateEngine');

@Provider()
class TemplateEngine {
  render(template: string, vars: Record<string, string>): string {
    return template.replace(/\{\{(\w+)\}\}/g, (_, key) => vars[key] || '');
  }
}

@Prompt({
  name: 'email-draft',
  description: 'Draft an email',
  arguments: [
    { name: 'recipient', required: true },
    { name: 'subject', required: true },
    { name: 'tone', required: false },
  ],
})
class EmailDraftPrompt extends PromptContext {
  private template = `
Draft a professional email with the following details:
- To: {{recipient}}
- Subject: {{subject}}
- Tone: {{tone}}

Please write a complete email including:
1. Appropriate greeting
2. Clear body content
3. Professional closing
`;

  async execute(args: Record<string, string>) {
    const engine = this.get(TemplateEngineToken);
    const promptText = engine.render(this.template, {
      recipient: args.recipient,
      subject: args.subject,
      tone: args.tone || 'professional',
    });

    return {
      description: `Email draft: ${args.subject}`,
      messages: [
        {
          role: 'user',
          content: { type: 'text', text: promptText },
        },
      ],
    };
  }
}

Full Example

import { Prompt, PromptContext, App, FrontMcp } from '@frontmcp/sdk';

@Prompt({
  name: 'api-documentation',
  title: 'API Documentation Generator',
  description: 'Generate API documentation from endpoint details',
  arguments: [
    { name: 'endpoint', description: 'API endpoint path', required: true },
    { name: 'method', description: 'HTTP method', required: true },
    { name: 'description', description: 'Endpoint description', required: true },
    { name: 'parameters', description: 'JSON parameters spec', required: false },
    { name: 'response', description: 'JSON response example', required: false },
  ],
})
class ApiDocumentationPrompt extends PromptContext {
  async execute(args: Record<string, string>) {
    this.mark('building');

    const sections: string[] = [
      `## ${args.method.toUpperCase()} ${args.endpoint}`,
      '',
      args.description,
      '',
    ];

    if (args.parameters) {
      sections.push('### Parameters');
      sections.push('```json');
      sections.push(args.parameters);
      sections.push('```');
      sections.push('');
    }

    if (args.response) {
      sections.push('### Response Example');
      sections.push('```json');
      sections.push(args.response);
      sections.push('```');
      sections.push('');
    }

    const context = sections.join('\n');

    return {
      description: `Documentation for ${args.method} ${args.endpoint}`,
      messages: [
        {
          role: 'user',
          content: {
            type: 'text',
            text: `You are a technical writer. Generate comprehensive API documentation in Markdown format.

Here is the endpoint information:

${context}

Please generate:
1. A clear description of what the endpoint does
2. Detailed parameter documentation with types and validation rules
3. Response schema documentation
4. Example requests using curl
5. Common error responses
6. Any relevant notes or warnings`,
          },
        },
      ],
    };
  }
}

@App({
  name: 'docs',
  prompts: [ApiDocumentationPrompt],
})
class DocsApp {}

@FrontMcp({
  info: { name: 'Documentation Generator', version: '1.0.0' },
  apps: [DocsApp],
})
export default class DocGenServer {}

@Prompt

Prompt decorator

PromptRegistry

Prompt registry

@Tool

Tool decorator

@Agent

Agent decorator