mcp-ts-template 2.1.7 β 2.2.0
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.
- package/README.md +58 -49
- package/dist/index.js +336 -124
- package/package.json +5 -3
- package/dist/worker.js +0 -60707
package/README.md
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
|
|
6
6
|
<div align="center">
|
|
7
7
|
|
|
8
|
-
[](./CHANGELOG.md) [](https://github.com/modelcontextprotocol/modelcontextprotocol/blob/main/docs/specification/2025-06-18/changelog.mdx) [](https://modelcontextprotocol.io/) [](./LICENSE) [](https://github.com/cyanheads/mcp-ts-template/issues) [](https://www.typescriptlang.org/) [](https://bun.sh/) [](./coverage/lcov-report/)
|
|
9
9
|
|
|
10
10
|
</div>
|
|
11
11
|
|
|
@@ -14,6 +14,7 @@
|
|
|
14
14
|
## β¨ Features
|
|
15
15
|
|
|
16
16
|
- **Declarative Tools & Resources**: Define capabilities in single, self-contained files. The framework handles registration and execution.
|
|
17
|
+
- **Elicitation Support**: Tools can interactively prompt the user for missing parameters during execution, streamlining user workflows.
|
|
17
18
|
- **Robust Error Handling**: A unified `McpError` system ensures consistent, structured error responses across the server.
|
|
18
19
|
- **Pluggable Authentication**: Secure your server with zero-fuss support for `none`, `jwt`, or `oauth` modes.
|
|
19
20
|
- **Abstracted Storage**: Swap storage backends (`in-memory`, `filesystem`, `Supabase`, `Cloudflare KV/R2`) without changing business logic.
|
|
@@ -29,22 +30,25 @@
|
|
|
29
30
|
|
|
30
31
|
### Installation
|
|
31
32
|
|
|
32
|
-
1.
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
33
|
+
1.
|
|
34
|
+
**Clone the repository:**
|
|
35
|
+
```sh
|
|
36
|
+
git clone https://github.com/cyanheads/mcp-ts-template.git
|
|
37
|
+
```
|
|
38
|
+
2.
|
|
39
|
+
**Navigate into the directory:**
|
|
40
|
+
```sh
|
|
41
|
+
cd mcp-ts-template
|
|
42
|
+
```
|
|
43
|
+
3.
|
|
44
|
+
**Install dependencies:**
|
|
45
|
+
```sh
|
|
46
|
+
bun install
|
|
47
|
+
```
|
|
44
48
|
|
|
45
49
|
## π οΈ Understanding the Template: Tools & Resources
|
|
46
50
|
|
|
47
|
-
This template includes working examples of
|
|
51
|
+
This template includes working examples of tools and resources.
|
|
48
52
|
|
|
49
53
|
### 1. Example Tool: `template_echo_message`
|
|
50
54
|
|
|
@@ -56,7 +60,7 @@ This tool echoes back a message with optional formatting. You can find the full
|
|
|
56
60
|
```ts
|
|
57
61
|
// Located at: src/mcp-server/tools/definitions/template-echo-message.tool.ts
|
|
58
62
|
import { z } from 'zod';
|
|
59
|
-
import type { ToolDefinition } from '@/mcp-server/tools/utils/toolDefinition.js';
|
|
63
|
+
import type { SdkContext, ToolDefinition } from '@/mcp-server/tools/utils/toolDefinition.js';
|
|
60
64
|
import { withToolAuth } from '@/mcp-server/transports/auth/lib/withAuth.js';
|
|
61
65
|
import { type RequestContext, logger } from '@/utils/index.js';
|
|
62
66
|
|
|
@@ -86,7 +90,8 @@ const OutputSchema = z.object({
|
|
|
86
90
|
// 2. Implement the pure business logic for the tool.
|
|
87
91
|
async function echoToolLogic(
|
|
88
92
|
input: z.infer<typeof InputSchema>,
|
|
89
|
-
|
|
93
|
+
appContext: RequestContext,
|
|
94
|
+
sdkContext: SdkContext,
|
|
90
95
|
): Promise<z.infer<typeof OutputSchema>> {
|
|
91
96
|
// ... logic to format and repeat the message
|
|
92
97
|
const formattedMessage = input.message.toUpperCase(); // simplified for example
|
|
@@ -98,7 +103,7 @@ async function echoToolLogic(
|
|
|
98
103
|
export const echoTool: ToolDefinition<typeof InputSchema, typeof OutputSchema> =
|
|
99
104
|
{
|
|
100
105
|
name: 'template_echo_message', // The official tool name
|
|
101
|
-
title: 'Echo Message',
|
|
106
|
+
title: 'Template Echo Message',
|
|
102
107
|
description:
|
|
103
108
|
'Echoes a message back with optional formatting and repetition.',
|
|
104
109
|
inputSchema: InputSchema,
|
|
@@ -107,7 +112,7 @@ export const echoTool: ToolDefinition<typeof InputSchema, typeof OutputSchema> =
|
|
|
107
112
|
};
|
|
108
113
|
```
|
|
109
114
|
|
|
110
|
-
The `echoTool` is registered in `src/mcp-server/tools/definitions/index.ts`, making it available to the server on startup.
|
|
115
|
+
The `echoTool` is registered in `src/mcp-server/tools/definitions/index.ts`, making it available to the server on startup. For an example of how to use the new elicitation feature, see `template_madlibs_elicitation.tool.ts`.
|
|
111
116
|
|
|
112
117
|
</details>
|
|
113
118
|
|
|
@@ -175,14 +180,14 @@ Like the tool, `echoResourceDefinition` is registered in `src/mcp-server/resourc
|
|
|
175
180
|
|
|
176
181
|
All configuration is centralized and validated at startup in `src/config/index.ts`. Key environment variables in your `.env` file include:
|
|
177
182
|
|
|
178
|
-
| Variable
|
|
179
|
-
|
|
|
180
|
-
| `MCP_TRANSPORT_TYPE`
|
|
181
|
-
| `MCP_HTTP_PORT`
|
|
182
|
-
| `MCP_AUTH_MODE`
|
|
183
|
+
| Variable | Description | Default |
|
|
184
|
+
| :--- | :--- | :--- |
|
|
185
|
+
| `MCP_TRANSPORT_TYPE` | The transport to use: `stdio` or `http`. | `http` |
|
|
186
|
+
| `MCP_HTTP_PORT` | The port for the HTTP server. | `3010` |
|
|
187
|
+
| `MCP_AUTH_MODE` | Authentication mode: `none`, `jwt`, or `oauth`. | `none` |
|
|
183
188
|
| `STORAGE_PROVIDER_TYPE` | Storage backend: `in-memory`, `filesystem`, `supabase`, `cloudflare-kv`, `r2`. | `in-memory` |
|
|
184
|
-
| `OTEL_ENABLED`
|
|
185
|
-
| `LOG_LEVEL`
|
|
189
|
+
| `OTEL_ENABLED` | Set to `true` to enable OpenTelemetry. | `false` |
|
|
190
|
+
| `LOG_LEVEL` | The minimum level for logging. | `info` |
|
|
186
191
|
|
|
187
192
|
### Authentication & Authorization
|
|
188
193
|
|
|
@@ -219,44 +224,48 @@ All configuration is centralized and validated at startup in `src/config/index.t
|
|
|
219
224
|
- **Run checks and tests**:
|
|
220
225
|
```sh
|
|
221
226
|
bun devcheck # Lints, formats, type-checks, and more
|
|
222
|
-
bun test
|
|
227
|
+
bun test # Runs the test suite
|
|
223
228
|
```
|
|
224
229
|
|
|
225
230
|
### Cloudflare Workers
|
|
226
231
|
|
|
227
|
-
1.
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
232
|
+
1.
|
|
233
|
+
**Build the Worker bundle**:
|
|
234
|
+
```sh
|
|
235
|
+
bun build:worker
|
|
236
|
+
```
|
|
237
|
+
2.
|
|
238
|
+
**Run locally with Wrangler**:
|
|
239
|
+
```sh
|
|
240
|
+
bun deploy:dev
|
|
241
|
+
```
|
|
242
|
+
3.
|
|
243
|
+
**Deploy to Cloudflare**:
|
|
244
|
+
`sh
|
|
245
|
+
bun deploy:prod
|
|
246
|
+
`
|
|
247
|
+
> **Note**: The `wrangler.toml` file is pre-configured to enable `nodejs_compat` for best results.
|
|
240
248
|
|
|
241
249
|
## π Project Structure
|
|
242
250
|
|
|
243
|
-
| Directory
|
|
244
|
-
|
|
|
245
|
-
| `src/mcp-server/tools/definitions`
|
|
251
|
+
| Directory | Purpose & Contents |
|
|
252
|
+
| :--- | :--- |
|
|
253
|
+
| `src/mcp-server/tools/definitions` | Your tool definitions (`*.tool.ts`). This is where you add new capabilities. |
|
|
246
254
|
| `src/mcp-server/resources/definitions` | Your resource definitions (`*.resource.ts`). This is where you add new data sources. |
|
|
247
|
-
| `src/mcp-server/transports`
|
|
248
|
-
| `src/storage`
|
|
249
|
-
| `src/services`
|
|
250
|
-
| `src/container`
|
|
251
|
-
| `src/utils`
|
|
252
|
-
| `src/config`
|
|
253
|
-
| `tests/`
|
|
255
|
+
| `src/mcp-server/transports` | Implementations for HTTP and STDIO transports, including auth middleware. |
|
|
256
|
+
| `src/storage` | The `StorageService` abstraction and all storage provider implementations. |
|
|
257
|
+
| `src/services` | Integrations with external services (e.g., the default OpenRouter LLM provider). |
|
|
258
|
+
| `src/container` | Dependency injection container registrations and tokens. |
|
|
259
|
+
| `src/utils` | Core utilities for logging, error handling, performance, security, and telemetry. |
|
|
260
|
+
| `src/config` | Environment variable parsing and validation with Zod. |
|
|
261
|
+
| `tests/` | Unit and integration tests, mirroring the `src/` directory structure. |
|
|
254
262
|
|
|
255
263
|
## π§βπ» Agent Development Guide
|
|
256
264
|
|
|
257
265
|
For a strict set of rules when using this template with an AI agent, please refer to **`AGENTS.md`**. Key principles include:
|
|
258
266
|
|
|
259
267
|
- **Logic Throws, Handlers Catch**: Never use `try/catch` in your tool/resource `logic`. Throw an `McpError` instead.
|
|
268
|
+
- **Use Elicitation for Missing Input**: If a tool requires user input that wasn't provided, use the `elicitInput` function from the `SdkContext` to ask the user for it.
|
|
260
269
|
- **Pass the Context**: Always pass the `RequestContext` object through your call stack.
|
|
261
270
|
- **Use the Barrel Exports**: Register new tools and resources only in the `index.ts` barrel files.
|
|
262
271
|
|