tmcp 1.0.1 โ†’ 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.md +277 -0
  2. package/package.json +3 -2
package/README.md ADDED
@@ -0,0 +1,277 @@
1
+ # tmcp
2
+
3
+ A lightweight, schema-agnostic Model Context Protocol (MCP) server implementation with unified API design.
4
+
5
+ ## Why tmcp?
6
+
7
+ tmcp offers significant advantages over the official MCP SDK:
8
+
9
+ - **๐Ÿ”„ Schema Agnostic**: Works with any validation library through adapters
10
+ - **๐Ÿ“ฆ No Weird Dependencies**: Minimal footprint with only essential dependencies (looking at you `express`)
11
+ - **๐ŸŽฏ Unified API**: Consistent, intuitive interface across all MCP capabilities
12
+ - **๐Ÿ”Œ Extensible**: Easy to add support for new schema libraries
13
+ - **โšก Lightweight**: No bloat, just what you need
14
+
15
+ ## Supported Schema Libraries
16
+
17
+ tmcp works with all major schema validation libraries through its adapter system:
18
+
19
+ - **Zod** - `@tmcpkit/adapter-zod`
20
+ - **Valibot** - `@tmcpkit/adapter-valibot`
21
+ - **ArkType** - `@tmcpkit/adapter-arktype`
22
+ - **Effect Schema** - `@tmcpkit/adapter-effect`
23
+ - **Zod v3** - `@tmcpkit/adapter-zod-v3`
24
+
25
+ ## Installation
26
+
27
+ ```bash
28
+ pnpm install tmcp
29
+ # Choose your preferred schema library adapter
30
+ รจnpm install @tmcpkit/adapter-zod zod
31
+ ```
32
+
33
+ ## Quick Start
34
+
35
+ ```javascript
36
+ import { McpServer } from 'tmcp';
37
+ import { ZodJsonSchemaAdapter } from '@tmcpkit/adapter-zod';
38
+ import { z } from 'zod';
39
+
40
+ const adapter = new ZodJsonSchemaAdapter();
41
+ const server = new McpServer(
42
+ {
43
+ name: 'my-server',
44
+ version: '1.0.0',
45
+ description: 'My awesome MCP server',
46
+ },
47
+ {
48
+ adapter,
49
+ capabilities: {
50
+ tools: { listChanged: true },
51
+ prompts: { listChanged: true },
52
+ resources: { listChanged: true },
53
+ },
54
+ },
55
+ );
56
+
57
+ // Define a tool with type-safe schema
58
+ server.tool(
59
+ {
60
+ name: 'calculate',
61
+ description: 'Perform mathematical calculations',
62
+ schema: z.object({
63
+ operation: z.enum(['add', 'subtract', 'multiply', 'divide']),
64
+ a: z.number(),
65
+ b: z.number(),
66
+ }),
67
+ },
68
+ async ({ operation, a, b }) => {
69
+ switch (operation) {
70
+ case 'add':
71
+ return a + b;
72
+ case 'subtract':
73
+ return a - b;
74
+ case 'multiply':
75
+ return a * b;
76
+ case 'divide':
77
+ return a / b;
78
+ }
79
+ },
80
+ );
81
+
82
+ // Process incoming requests
83
+ server.receive(request);
84
+ ```
85
+
86
+ ## API Reference
87
+
88
+ ### McpServer
89
+
90
+ The main server class that handles MCP protocol communications.
91
+
92
+ #### Constructor
93
+
94
+ ```javascript
95
+ new McpServer(serverInfo, options);
96
+ ```
97
+
98
+ - `serverInfo`: Server metadata (name, version, description)
99
+ - `options`: Configuration object with adapter and capabilities
100
+
101
+ #### Methods
102
+
103
+ ##### `tool(definition, handler)`
104
+
105
+ Register a tool with optional schema validation.
106
+
107
+ ```javascript
108
+ server.tool(
109
+ {
110
+ name: 'tool-name',
111
+ description: 'Tool description',
112
+ schema: yourSchema, // optional
113
+ },
114
+ async (input) => {
115
+ // Tool implementation
116
+ return result;
117
+ },
118
+ );
119
+ ```
120
+
121
+ ##### `prompt(definition, handler)`
122
+
123
+ Register a prompt template with optional schema validation.
124
+
125
+ ```javascript
126
+ server.prompt(
127
+ {
128
+ name: 'prompt-name',
129
+ description: 'Prompt description',
130
+ schema: yourSchema, // optional
131
+ complete: (arg, context) => ['completion1', 'completion2'] // optional
132
+ },
133
+ async (input) => {
134
+ // Prompt implementation
135
+ return { messages: [...] };
136
+ }
137
+ );
138
+ ```
139
+
140
+ ##### `resource(definition, handler)`
141
+
142
+ Register a static resource.
143
+
144
+ ```javascript
145
+ server.resource(
146
+ {
147
+ name: 'resource-name',
148
+ description: 'Resource description',
149
+ uri: 'file://path/to/resource'
150
+ },
151
+ async (uri, params) => {
152
+ // Resource implementation
153
+ return { contents: [...] };
154
+ }
155
+ );
156
+ ```
157
+
158
+ ##### `template(definition, handler)`
159
+
160
+ Register a URI template for dynamic resources.
161
+
162
+ ```javascript
163
+ server.template(
164
+ {
165
+ name: 'template-name',
166
+ description: 'Template description',
167
+ uri: 'file://path/{id}/resource',
168
+ complete: (arg, context) => ['id1', 'id2'] // optional
169
+ },
170
+ async (uri, params) => {
171
+ // Template implementation using params.id
172
+ return { contents: [...] };
173
+ }
174
+ );
175
+ ```
176
+
177
+ ##### `receive(request)`
178
+
179
+ Process an incoming MCP request.
180
+
181
+ ```javascript
182
+ const response = server.receive(jsonRpcRequest);
183
+ ```
184
+
185
+ ## Advanced Examples
186
+
187
+ ### Multiple Schema Libraries
188
+
189
+ ```javascript
190
+ // Use different schemas for different tools
191
+ import { z } from 'zod';
192
+ import * as v from 'valibot';
193
+
194
+ server.tool(
195
+ {
196
+ name: 'zod-tool',
197
+ schema: z.object({ name: z.string() }),
198
+ },
199
+ async ({ name }) => `Hello ${name}`,
200
+ );
201
+
202
+ server.tool(
203
+ {
204
+ name: 'valibot-tool',
205
+ schema: v.object({ age: v.number() }),
206
+ },
207
+ async ({ age }) => `Age: ${age}`,
208
+ );
209
+ ```
210
+
211
+ ### Resource Templates with Completion
212
+
213
+ ```javascript
214
+ server.template(
215
+ {
216
+ name: 'user-profile',
217
+ description: 'Get user profile by ID',
218
+ uri: 'users/{userId}/profile',
219
+ complete: (arg, context) => {
220
+ // Provide completions for userId parameter
221
+ return ['user1', 'user2', 'user3'];
222
+ },
223
+ },
224
+ async (uri, params) => {
225
+ const user = await getUserById(params.userId);
226
+ return {
227
+ contents: [
228
+ {
229
+ uri,
230
+ mimeType: 'application/json',
231
+ text: JSON.stringify(user),
232
+ },
233
+ ],
234
+ };
235
+ },
236
+ );
237
+ ```
238
+
239
+ ### Complex Validation
240
+
241
+ ```javascript
242
+ const complexSchema = z.object({
243
+ user: z.object({
244
+ name: z.string().min(1),
245
+ email: z.string().email(),
246
+ age: z.number().min(18).max(120),
247
+ }),
248
+ preferences: z
249
+ .object({
250
+ theme: z.enum(['light', 'dark']),
251
+ notifications: z.boolean(),
252
+ })
253
+ .optional(),
254
+ tags: z.array(z.string()).default([]),
255
+ });
256
+
257
+ server.tool(
258
+ {
259
+ name: 'create-user',
260
+ description: 'Create a new user with preferences',
261
+ schema: complexSchema,
262
+ },
263
+ async (input) => {
264
+ // Input is fully typed and validated
265
+ const { user, preferences, tags } = input;
266
+ return await createUser(user, preferences, tags);
267
+ },
268
+ );
269
+ ```
270
+
271
+ ## Contributing
272
+
273
+ Contributions are welcome! Please see our [contributing guidelines](../../CONTRIBUTING.md) for details.
274
+
275
+ ## License
276
+
277
+ MIT ยฉ Paolo Ricciuti
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tmcp",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "description": "The main tmcp library",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
@@ -34,6 +34,7 @@
34
34
  "scripts": {
35
35
  "test": "echo \"Error: no test specified\" && exit 1",
36
36
  "typecheck": "tsc --noEmit",
37
- "generate:types": "rm -f ./src/*.d.ts && tsc ./src/*.js --declaration --emitDeclarationOnly --target esnext --allowJs --moduleresolution nodenext --module nodenext && publint"
37
+ "generate:types": "rm -f ./src/*.d.ts && tsc ./src/*.js --declaration --emitDeclarationOnly --target esnext --allowJs --moduleresolution nodenext --module nodenext && publint",
38
+ "prepublish": "pnpm generate:types"
38
39
  }
39
40
  }