alepha@docs:~/docs/packages/alepha$
cat mcp.md
2 min read
Last commit:

#Alepha - Mcp

#Installation

Part of the alepha package. Import from alepha/mcp.

npm install alepha

#Overview

Core MCP module with primitives and server provider.

This module registers the $tool, $resource, and $prompt primitives and the McpServerProvider. You need to add a transport module (AlephaMcpStdio or AlephaMcpSse) for actual communication.

ts
 1import { Alepha, run } from "alepha"; 2import { AlephaMcp, AlephaMcpStdio, $tool, t } from "alepha/mcp"; 3  4class MyMcpServer { 5  add = $tool({ 6    description: "Add two numbers", 7    schema: { 8      params: t.object({ a: t.number(), b: t.number() }), 9      result: t.number(),10    },11    handler: async ({ params }) => params.a + params.b,12  });13}14 15run(16  Alepha.create()17    .with(AlephaMcp)18    .with(AlephaMcpStdio)19    .with(MyMcpServer)20);

#API Reference

#Primitives

Primitives are functions that define and configure various aspects of your application. They follow the convention of starting with $ and return configured primitive instances.

For more details, see the Primitives documentation.

#$prompt()

Creates an MCP prompt primitive for defining reusable prompt templates.

Prompts allow you to define templated messages that can be filled in with arguments at runtime. They're useful for creating consistent interaction patterns.

ts
 1class Prompts { 2  greeting = $prompt({ 3    description: "Generate a personalized greeting", 4    args: t.object({ 5      name: t.text({ description: "Name of the person to greet" }), 6      style: t.optional(t.enum(["formal", "casual"])), 7    }), 8    handler: async ({ args }) => [ 9      {10        role: "user",11        content: args.style === "formal"12          ? `Please greet ${args.name} in a formal manner.`13          : `Say hi to ${args.name}!`,14      },15    ],16  });17 18  codeReview = $prompt({19    description: "Request a code review",20    args: t.object({21      code: t.text({ description: "The code to review" }),22      language: t.text({ description: "Programming language" }),23    }),24    handler: async ({ args }) => [25      {26        role: "user",27        content: `Please review this ${args.language} code:

${args.code}`,28      },29    ],30  });31}

#$resource()

Creates an MCP resource primitive for exposing read-only data.

Resources represent any kind of data that an LLM might want to read, such as files, database records, API responses, or computed data.

Key Features

  • URI-based identification for resources
  • Support for text and binary content
  • MIME type specification
  • Lazy loading via handler function
ts
 1class ProjectResources { 2  readme = $resource({ 3    uri: "file:///readme", 4    description: "Project README file", 5    mimeType: "text/markdown", 6    handler: async () => ({ 7      text: await fs.readFile("README.md", "utf-8"), 8    }), 9  });10 11  config = $resource({12    uri: "config://app",13    name: "Application Configuration",14    mimeType: "application/json",15    handler: async () => ({16      text: JSON.stringify(this.configService.getConfig()),17    }),18  });19}

#$tool()

Creates an MCP tool primitive for defining callable functions.

Tools are the primary way for LLMs to interact with external systems through MCP. Each tool has a name, description, typed parameters, and a handler function.

Key Features

  • Full TypeScript inference for parameters and results
  • Automatic schema validation using TypeBox
  • JSON Schema generation for MCP protocol
  • Integration with MCP server provider
ts
 1class CalculatorTools { 2  add = $tool({ 3    description: "Add two numbers together", 4    schema: { 5      params: t.object({ 6        a: t.number(), 7        b: t.number(), 8      }), 9      result: t.number(),10    },11    handler: async ({ params }) => {12      return params.a + params.b;13    },14  });15 16  greet = $tool({17    description: "Generate a greeting message",18    schema: {19      params: t.object({20        name: t.text(),21      }),22      result: t.text(),23    },24    handler: async ({ params }) => {25      return `Hello, ${params.name}!`;26    },27  });28}

#Providers

Providers are classes that encapsulate specific functionality and can be injected into your application. They handle initialization, configuration, and lifecycle management.

For more details, see the Providers documentation.

#McpServerProvider

Core MCP server provider that handles protocol messages.

This provider maintains registries of tools, resources, and prompts, and routes incoming JSON-RPC requests to the appropriate handlers.

It is transport-agnostic - actual communication is handled by transport providers like StdioMcpTransport or SseMcpTransport.

On This Page
No headings found...
ready
mainTypeScript
UTF-8packages_alepha_mcp.md