|
1 | 1 | # mcp-framework
|
2 | 2 |
|
3 |
| -A framework for building Model Context Protocol (MCP) servers with automatic tool loading and management in Typescript. |
| 3 | +A framework for building Model Context Protocol (MCP) servers elegantly in TypeScript. |
4 | 4 |
|
5 | 5 | Get started fast with mcp-framework ⚡⚡⚡
|
6 | 6 |
|
7 | 7 | ## Features
|
8 | 8 |
|
9 |
| -- 🛠️ Automatic tool discovery and loading |
10 |
| -- 🏗️ Base tool implementation with helper methods |
11 |
| -- ⚙️ Configurable tool directory and exclusions |
12 |
| -- 🔒 Type-safe tool validation |
| 9 | +- 🛠️ Automatic directory-based discovery and loading for tools, prompts, and resources |
| 10 | +- 🏗️ Powerful abstractions |
13 | 11 | - 🚀 Simple server setup and configuration
|
14 |
| -- 🐛 Built-in error handling and logging |
15 | 12 |
|
16 | 13 | ## Installation
|
17 | 14 |
|
18 | 15 | ```bash
|
19 |
| -npm install mcp-framework @modelcontextprotocol/sdk |
| 16 | +npm install mcp-framework |
20 | 17 | ```
|
21 | 18 |
|
22 | 19 | ## Quick Start
|
23 | 20 |
|
24 |
| -1. Create your MCP server: |
| 21 | +### 1. Create your MCP server: |
25 | 22 |
|
26 | 23 | ```typescript
|
27 | 24 | import { MCPServer } from "mcp-framework";
|
28 | 25 |
|
29 |
| -const server = new MCPServer({ |
30 |
| - name: "my-mcp-server", |
31 |
| - version: "1.0.0", |
32 |
| - toolsDir: "./dist/tools", // Optional: defaults to dist/tools |
33 |
| -}); |
| 26 | +const server = new MCPServer(); |
34 | 27 |
|
35 | 28 | server.start().catch((error) => {
|
36 | 29 | console.error("Server failed to start:", error);
|
37 | 30 | process.exit(1);
|
38 | 31 | });
|
39 | 32 | ```
|
40 | 33 |
|
41 |
| -2. Create a tool by extending BaseToolImplementation: |
| 34 | +### 2. Create a Tool: |
42 | 35 |
|
43 | 36 | ```typescript
|
44 |
| -import { BaseToolImplementation } from "mcp-framework"; |
45 |
| -import { Tool } from "@modelcontextprotocol/sdk/types.js"; |
| 37 | +import { MCPTool } from "mcp-framework"; |
| 38 | +import { z } from "zod"; |
| 39 | + |
| 40 | +interface ExampleInput { |
| 41 | + message: string; |
| 42 | +} |
46 | 43 |
|
47 |
| -class ExampleTool extends BaseToolImplementation { |
| 44 | +class ExampleTool extends MCPTool<ExampleInput> { |
48 | 45 | name = "example_tool";
|
49 |
| - toolDefinition: Tool = { |
50 |
| - name: this.name, |
51 |
| - description: "An example tool", |
52 |
| - inputSchema: { |
53 |
| - type: "object", |
54 |
| - properties: { |
55 |
| - input: { |
56 |
| - type: "string", |
57 |
| - description: "Input parameter", |
58 |
| - }, |
59 |
| - }, |
| 46 | + description = "An example tool that processes messages"; |
| 47 | + |
| 48 | + schema = { |
| 49 | + message: { |
| 50 | + type: z.string(), |
| 51 | + description: "Message to process", |
60 | 52 | },
|
61 | 53 | };
|
62 | 54 |
|
63 |
| - async toolCall(request: any) { |
64 |
| - try { |
65 |
| - const input = request.params.arguments?.input; |
66 |
| - if (!input) { |
67 |
| - throw new Error("Missing input parameter"); |
68 |
| - } |
69 |
| - |
70 |
| - const result = `Processed: ${input}`; |
71 |
| - return this.createSuccessResponse(result); |
72 |
| - } catch (error) { |
73 |
| - return this.createErrorResponse(error); |
74 |
| - } |
| 55 | + async execute(input: ExampleInput) { |
| 56 | + return `Processed: ${input.message}`; |
75 | 57 | }
|
76 | 58 | }
|
77 | 59 |
|
78 | 60 | export default ExampleTool;
|
79 | 61 | ```
|
80 | 62 |
|
81 |
| -## Configuration |
| 63 | +### 3. Create a Prompt: |
82 | 64 |
|
83 |
| -### MCPServer Options |
| 65 | +```typescript |
| 66 | +import { MCPPrompt } from "mcp-framework"; |
| 67 | +import { z } from "zod"; |
| 68 | + |
| 69 | +interface GreetingInput { |
| 70 | + name: string; |
| 71 | + language?: string; |
| 72 | +} |
84 | 73 |
|
85 |
| -- `name`: Server name |
86 |
| -- `version`: Server version |
87 |
| -- `toolsDir`: Directory containing tool files (optional) |
88 |
| -- `excludeTools`: Array of patterns for files to exclude (optional) |
| 74 | +class GreetingPrompt extends MCPPrompt<GreetingInput> { |
| 75 | + name = "greeting"; |
| 76 | + description = "Generate a greeting in different languages"; |
89 | 77 |
|
90 |
| -### Project Structure |
| 78 | + schema = { |
| 79 | + name: { |
| 80 | + type: z.string(), |
| 81 | + description: "Name to greet", |
| 82 | + required: true, |
| 83 | + }, |
| 84 | + language: { |
| 85 | + type: z.string().optional(), |
| 86 | + description: "Language for greeting", |
| 87 | + required: false, |
| 88 | + }, |
| 89 | + }; |
| 90 | + |
| 91 | + async generateMessages({ name, language = "English" }: GreetingInput) { |
| 92 | + return [ |
| 93 | + { |
| 94 | + role: "user", |
| 95 | + content: { |
| 96 | + type: "text", |
| 97 | + text: `Generate a greeting for ${name} in ${language}`, |
| 98 | + }, |
| 99 | + }, |
| 100 | + ]; |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +export default GreetingPrompt; |
| 105 | +``` |
| 106 | + |
| 107 | +### 4. Create a Resource: |
| 108 | + |
| 109 | +```typescript |
| 110 | +import { MCPResource, ResourceContent } from "mcp-framework"; |
| 111 | + |
| 112 | +class ConfigResource extends MCPResource { |
| 113 | + uri = "config://app/settings"; |
| 114 | + name = "Application Settings"; |
| 115 | + description = "Current application configuration"; |
| 116 | + mimeType = "application/json"; |
| 117 | + |
| 118 | + async read(): Promise<ResourceContent[]> { |
| 119 | + const config = { |
| 120 | + theme: "dark", |
| 121 | + language: "en", |
| 122 | + }; |
| 123 | + |
| 124 | + return [ |
| 125 | + { |
| 126 | + uri: this.uri, |
| 127 | + mimeType: this.mimeType, |
| 128 | + text: JSON.stringify(config, null, 2), |
| 129 | + }, |
| 130 | + ]; |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +export default ConfigResource; |
| 135 | +``` |
| 136 | + |
| 137 | +## Project Structure |
91 | 138 |
|
92 | 139 | ```
|
93 | 140 | your-project/
|
94 | 141 | ├── src/
|
95 |
| -│ ├── tools/ |
96 |
| -│ │ ├── ExampleTool.ts |
97 |
| -│ │ └── OtherTool.ts |
| 142 | +│ ├── tools/ # Tool implementations |
| 143 | +│ │ └── ExampleTool.ts |
| 144 | +│ ├── prompts/ # Prompt implementations |
| 145 | +│ │ └── GreetingPrompt.ts |
| 146 | +│ ├── resources/ # Resource implementations |
| 147 | +│ │ └── ConfigResource.ts |
98 | 148 | │ └── index.ts
|
99 | 149 | ├── package.json
|
100 | 150 | └── tsconfig.json
|
101 | 151 | ```
|
102 | 152 |
|
| 153 | +## Automatic Feature Discovery |
| 154 | + |
| 155 | +The framework automatically discovers and loads: |
| 156 | + |
| 157 | +- Tools from the `src/tools` directory |
| 158 | +- Prompts from the `src/prompts` directory |
| 159 | +- Resources from the `src/resources` directory |
| 160 | + |
| 161 | +Each feature should be in its own file and export a default class that extends the appropriate base class: |
| 162 | + |
| 163 | +- `MCPTool` for tools |
| 164 | +- `MCPPrompt` for prompts |
| 165 | +- `MCPResource` for resources |
| 166 | + |
| 167 | +### Base Classes |
| 168 | + |
| 169 | +#### MCPTool |
| 170 | + |
| 171 | +- Handles input validation using Zod |
| 172 | +- Provides error handling and response formatting |
| 173 | +- Includes fetch helper for HTTP requests |
| 174 | + |
| 175 | +#### MCPPrompt |
| 176 | + |
| 177 | +- Manages prompt arguments and validation |
| 178 | +- Generates message sequences for LLM interactions |
| 179 | +- Supports dynamic prompt templates |
| 180 | + |
| 181 | +#### MCPResource |
| 182 | + |
| 183 | +- Exposes data through URI-based system |
| 184 | +- Supports text and binary content |
| 185 | +- Optional subscription capabilities for real-time updates |
| 186 | + |
| 187 | +## Type Safety |
| 188 | + |
| 189 | +All features use Zod for runtime type validation and TypeScript for compile-time type checking. Define your input schemas using Zod types: |
| 190 | + |
| 191 | +```typescript |
| 192 | +schema = { |
| 193 | + parameter: { |
| 194 | + type: z.string().email(), |
| 195 | + description: "User email address", |
| 196 | + }, |
| 197 | + count: { |
| 198 | + type: z.number().min(1).max(100), |
| 199 | + description: "Number of items", |
| 200 | + }, |
| 201 | +}; |
| 202 | +``` |
| 203 | + |
103 | 204 | ## License
|
104 | 205 |
|
105 | 206 | MIT
|
0 commit comments