MCP Apps Kit - v0.5.0
    Preparing search index...

    Interface Plugin<TConfig>

    Plugin definition with lifecycle and execution hooks

    Plugins extend app behavior without modifying tool handlers. All hooks are optional - implement only what you need.

    const loggingPlugin: Plugin<{ level: string }> = {
    name: 'logging',
    version: '1.0.0',
    configSchema: z.object({ level: z.enum(['debug', 'info', 'warn', 'error']) }),
    config: { level: 'info' },

    onInit: async (context) => {
    console.log('Plugin initialized with tools:', Object.keys(context.tools));
    },

    beforeToolCall: async (context) => {
    console.log(`[${new Date().toISOString()}] Tool called: ${context.toolName}`);
    },
    };
    interface Plugin<TConfig = unknown> {
        config?: TConfig;
        configSchema?: ZodType<TConfig>;
        name: string;
        version?: string;
        afterToolCall?(
            context: ToolCallContext,
            result: unknown,
        ): void | Promise<void>;
        beforeToolCall?(context: ToolCallContext): void | Promise<void>;
        onInit?(context: PluginInitContext): void | Promise<void>;
        onRequest?(context: RequestContext): void | Promise<void>;
        onResponse?(context: ResponseContext): void | Promise<void>;
        onShutdown?(context: PluginShutdownContext): void | Promise<void>;
        onStart?(context: PluginStartContext): void | Promise<void>;
        onToolError?(context: ToolCallContext, error: Error): void | Promise<void>;
        onUILoad?(context: UILoadContext): void | Promise<void>;
    }

    Type Parameters

    • TConfig = unknown

      Inferred from configSchema if provided

    Index

    Properties

    config?: TConfig

    Plugin configuration

    If configSchema is provided, this will be validated at plugin creation.

    configSchema?: ZodType<TConfig>

    Zod schema for config validation

    If provided, config will be validated and TConfig will be inferred.

    name: string

    Unique plugin identifier

    Used for logging, debugging, and error messages. Must be unique within an app.

    version?: string

    Plugin version (semantic versioning recommended)

    Optional but helpful for debugging.

    Methods

    • Called after tool handler succeeds

      If this hook throws, error is logged but response is sent normally.

      Parameters

      • context: ToolCallContext
      • result: unknown

      Returns void | Promise<void>

    • Called before tool handler executes

      Executes after middleware chain but before handler. If this hook throws, error is logged but tool execution continues.

      Parameters

      • context: ToolCallContext

      Returns void | Promise<void>

    • Called when app is initialized (before server starts)

      If this hook throws, app initialization fails.

      Parameters

      • context: PluginInitContext

      Returns void | Promise<void>

    • Called when HTTP request received (before routing)

      Not available for stdio transport. If this hook throws, error is logged but request continues.

      Parameters

      • context: RequestContext

      Returns void | Promise<void>

    • Called before HTTP response sent

      Not available for stdio transport. If this hook throws, error is logged but response is sent normally.

      Parameters

      • context: ResponseContext

      Returns void | Promise<void>

    • Called during graceful shutdown

      Hook is subject to shutdown timeout (default 30s).

      Parameters

      • context: PluginShutdownContext

      Returns void | Promise<void>

    • Called when server starts successfully

      If this hook throws, error is logged but server continues.

      Parameters

      • context: PluginStartContext

      Returns void | Promise<void>

    • Called when tool handler throws error

      Cannot prevent error from propagating to client. If this hook throws, error is logged separately.

      Parameters

      • context: ToolCallContext
      • error: Error

      Returns void | Promise<void>

    • Called when UI resource is loaded

      If this hook throws, error is logged but UI is served normally.

      Parameters

      • context: UILoadContext

      Returns void | Promise<void>