@agentailor/create-mcp-server 0.7.1 → 0.8.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 +27 -3
- package/dist/assets/skills/tool-design/SKILL.md +107 -0
- package/dist/assets/skills/tool-design/references/examples.md +273 -0
- package/dist/assets/skills/tool-design/references/principles.md +261 -0
- package/dist/assets/skills/tool-design/references/testing.md +195 -0
- package/dist/cli.d.ts +1 -0
- package/dist/cli.js +4 -2
- package/dist/cli.test.js +19 -0
- package/dist/index.js +1 -0
- package/dist/interactive.js +8 -0
- package/dist/project-generator.d.ts +1 -0
- package/dist/project-generator.js +27 -4
- package/dist/skills.d.ts +43 -0
- package/dist/skills.js +64 -0
- package/dist/skills.test.d.ts +1 -0
- package/dist/skills.test.js +85 -0
- package/dist/templates/common/agents.md.d.ts +9 -0
- package/dist/templates/common/agents.md.js +221 -0
- package/dist/templates/common/agents.md.test.d.ts +1 -0
- package/dist/templates/common/agents.md.test.js +142 -0
- package/dist/templates/common/package.json.js +19 -8
- package/dist/templates/common/tsconfig.json.d.ts +3 -1
- package/dist/templates/common/tsconfig.json.js +8 -2
- package/dist/templates/fastmcp/index.js +5 -3
- package/dist/templates/fastmcp/readme.js +13 -0
- package/dist/templates/sdk/stateful/auth.js +5 -1
- package/dist/templates/sdk/stateful/auth.test.js +6 -2
- package/dist/templates/sdk/stateful/index.d.ts +6 -0
- package/dist/templates/sdk/stateful/index.js +7 -1
- package/dist/templates/sdk/stateful/readme.js +1 -1
- package/dist/templates/sdk/stateful/templates.test.js +6 -7
- package/dist/templates/sdk/stateless/index.d.ts +6 -0
- package/dist/templates/sdk/stateless/index.js +10 -1
- package/dist/templates/sdk/stateless/prompts.d.ts +1 -0
- package/dist/templates/sdk/stateless/prompts.js +32 -0
- package/dist/templates/sdk/stateless/readme.js +72 -13
- package/dist/templates/sdk/stateless/resources.d.ts +1 -0
- package/dist/templates/sdk/stateless/resources.js +49 -0
- package/dist/templates/sdk/stateless/server-test.d.ts +1 -0
- package/dist/templates/sdk/stateless/server-test.js +175 -0
- package/dist/templates/sdk/stateless/server.js +14 -68
- package/dist/templates/sdk/stateless/store-test.d.ts +1 -0
- package/dist/templates/sdk/stateless/store-test.js +38 -0
- package/dist/templates/sdk/stateless/store.d.ts +1 -0
- package/dist/templates/sdk/stateless/store.js +107 -0
- package/dist/templates/sdk/stateless/templates.test.js +160 -14
- package/dist/templates/sdk/stateless/tools.d.ts +1 -0
- package/dist/templates/sdk/stateless/tools.js +172 -0
- package/dist/templates/sdk/stdio/index.d.ts +6 -0
- package/dist/templates/sdk/stdio/index.js +6 -0
- package/dist/templates/sdk/stdio/readme.js +64 -11
- package/dist/templates/sdk/stdio/templates.test.js +11 -13
- package/package.json +2 -2
|
@@ -10,9 +10,11 @@ server.start({
|
|
|
10
10
|
}
|
|
11
11
|
const stateless = options?.stateless ?? false;
|
|
12
12
|
const statelessConfig = stateless ? '\n stateless: true,' : '';
|
|
13
|
-
return `
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
return `import { config } from 'dotenv';
|
|
14
|
+
import { server } from './server.js';
|
|
15
|
+
|
|
16
|
+
// quiet: dotenv >=18 logs a summary line on load; keep startup output clean.
|
|
17
|
+
config({ quiet: true });
|
|
16
18
|
|
|
17
19
|
const PORT = Number(process.env.PORT) || 3000;
|
|
18
20
|
|
|
@@ -118,6 +118,19 @@ ${projectName}/
|
|
|
118
118
|
- Add new tools, prompts, and resources in \`src/server.ts\`
|
|
119
119
|
- Modify transport configuration in \`src/index.ts\`
|
|
120
120
|
|
|
121
|
+
## Tool design skill
|
|
122
|
+
|
|
123
|
+
Agentailor's [tool-design](https://github.com/agentailor/skills) skill is
|
|
124
|
+
included at \`.agents/skills/tool-design/\`, so a coding agent working here has
|
|
125
|
+
the guidance on hand. Commit it so the whole team gets the same copy.
|
|
126
|
+
|
|
127
|
+
It is the version that shipped with the scaffolding CLI. To check for a newer
|
|
128
|
+
one — your call, not automatic:
|
|
129
|
+
|
|
130
|
+
\`\`\`bash
|
|
131
|
+
npx skills update -p -y
|
|
132
|
+
\`\`\`
|
|
133
|
+
|
|
121
134
|
## Learn More
|
|
122
135
|
|
|
123
136
|
- [FastMCP](https://github.com/punkpeye/fastmcp) - The framework powering this server
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export function getAuthTemplate() {
|
|
2
|
-
return `import 'dotenv
|
|
2
|
+
return `import { config } from 'dotenv';
|
|
3
3
|
import { createRemoteJWKSet, jwtVerify, type JWTPayload, type JWTVerifyGetKey } from 'jose';
|
|
4
4
|
import {
|
|
5
5
|
mcpAuthMetadataRouter,
|
|
@@ -10,6 +10,10 @@ import { OAuthError, OAuthErrorCode } from '@modelcontextprotocol/server';
|
|
|
10
10
|
import type { OAuthMetadata, OAuthTokenVerifier } from '@modelcontextprotocol/server';
|
|
11
11
|
import type { Express, RequestHandler } from 'express';
|
|
12
12
|
|
|
13
|
+
// Imports are hoisted, so this runs before index.ts's own config() call - load
|
|
14
|
+
// the env here too, or CONFIG below reads empty values.
|
|
15
|
+
config({ quiet: true });
|
|
16
|
+
|
|
13
17
|
// OAuth configuration from environment variables
|
|
14
18
|
const CONFIG = {
|
|
15
19
|
host: process.env.HOST || 'localhost',
|
|
@@ -7,9 +7,13 @@ describe('auth template', () => {
|
|
|
7
7
|
expect(template).toContain('OAUTH_ISSUER_URL');
|
|
8
8
|
expect(template).toContain('OAUTH_AUDIENCE');
|
|
9
9
|
});
|
|
10
|
-
it('should
|
|
10
|
+
it('should load dotenv quietly before reading CONFIG', () => {
|
|
11
11
|
const template = getAuthTemplate();
|
|
12
|
-
expect(template).toContain("import 'dotenv
|
|
12
|
+
expect(template).toContain("import { config } from 'dotenv'");
|
|
13
|
+
expect(template).toContain('config({ quiet: true })');
|
|
14
|
+
// Imports are hoisted, so auth.ts must load the env itself rather than
|
|
15
|
+
// relying on index.ts having done it.
|
|
16
|
+
expect(template.indexOf('config({ quiet: true })')).toBeLessThan(template.indexOf('const CONFIG'));
|
|
13
17
|
});
|
|
14
18
|
it('should import jose for JWT verification', () => {
|
|
15
19
|
const template = getAuthTemplate();
|
|
@@ -3,3 +3,9 @@ export { getIndexTemplate } from '../stateless/index.js';
|
|
|
3
3
|
export { getServerTemplate } from './server.js';
|
|
4
4
|
export { getReadmeTemplate } from './readme.js';
|
|
5
5
|
export { getAuthTemplate } from './auth.js';
|
|
6
|
+
export { getStoreTemplate } from '../stateless/store.js';
|
|
7
|
+
export { getToolsTemplate } from '../stateless/tools.js';
|
|
8
|
+
export { getPromptsTemplate } from '../stateless/prompts.js';
|
|
9
|
+
export { getResourcesTemplate } from '../stateless/resources.js';
|
|
10
|
+
export { getStoreTestTemplate } from '../stateless/store-test.js';
|
|
11
|
+
export { getServerTestTemplate } from '../stateless/server-test.js';
|
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
// Intentionally identical to the stateless template - see "Why stateless and
|
|
2
|
-
// stateful are identical" in
|
|
2
|
+
// stateful are identical" in AGENTS.md. Edit behaviour in stateless/, not here.
|
|
3
3
|
export { getIndexTemplate } from '../stateless/index.js';
|
|
4
4
|
export { getServerTemplate } from './server.js';
|
|
5
5
|
export { getReadmeTemplate } from './readme.js';
|
|
6
6
|
export { getAuthTemplate } from './auth.js';
|
|
7
|
+
export { getStoreTemplate } from '../stateless/store.js';
|
|
8
|
+
export { getToolsTemplate } from '../stateless/tools.js';
|
|
9
|
+
export { getPromptsTemplate } from '../stateless/prompts.js';
|
|
10
|
+
export { getResourcesTemplate } from '../stateless/resources.js';
|
|
11
|
+
export { getStoreTestTemplate } from '../stateless/store-test.js';
|
|
12
|
+
export { getServerTestTemplate } from '../stateless/server-test.js';
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
// Intentionally identical to the stateless template - see "Why stateless and
|
|
2
|
-
// stateful are identical" in
|
|
2
|
+
// stateful are identical" in AGENTS.md. Edit behaviour in stateless/, not here.
|
|
3
3
|
export { getReadmeTemplate } from '../stateless/readme.js';
|
|
@@ -25,14 +25,13 @@ describe('sdk/stateful templates', () => {
|
|
|
25
25
|
expect(template).toContain("from '@modelcontextprotocol/server'");
|
|
26
26
|
expect(template).not.toContain('@modelcontextprotocol/sdk');
|
|
27
27
|
});
|
|
28
|
-
|
|
28
|
+
// The server is re-exported from stateless/, so this only checks that the
|
|
29
|
+
// same composition arrives here - its detail is tested in stateless/.
|
|
30
|
+
it('should compose the shared primitives', () => {
|
|
29
31
|
const template = getServerTemplate(projectName);
|
|
30
|
-
expect(template).toContain('
|
|
31
|
-
expect(template).toContain('
|
|
32
|
-
expect(template).toContain('
|
|
33
|
-
expect(template).toContain('registerTool');
|
|
34
|
-
expect(template).toContain('greeting-resource');
|
|
35
|
-
expect(template).toContain('registerResource');
|
|
32
|
+
expect(template).toContain('registerTools(server)');
|
|
33
|
+
expect(template).toContain('registerPrompts(server)');
|
|
34
|
+
expect(template).toContain('registerResources(server)');
|
|
36
35
|
});
|
|
37
36
|
});
|
|
38
37
|
describe('getIndexTemplate', () => {
|
|
@@ -7,3 +7,9 @@ import type { SdkTemplateOptions } from '../../common/types.js';
|
|
|
7
7
|
export declare function getIndexTemplate(options?: SdkTemplateOptions): string;
|
|
8
8
|
export { getServerTemplate } from './server.js';
|
|
9
9
|
export { getReadmeTemplate } from './readme.js';
|
|
10
|
+
export { getStoreTemplate } from './store.js';
|
|
11
|
+
export { getToolsTemplate } from './tools.js';
|
|
12
|
+
export { getPromptsTemplate } from './prompts.js';
|
|
13
|
+
export { getResourcesTemplate } from './resources.js';
|
|
14
|
+
export { getStoreTestTemplate } from './store-test.js';
|
|
15
|
+
export { getServerTestTemplate } from './server-test.js';
|
|
@@ -36,13 +36,16 @@ main().catch((error) => {
|
|
|
36
36
|
? `
|
|
37
37
|
console.log(\`OAuth metadata available at \${getOAuthMetadataUrl()}\`);`
|
|
38
38
|
: '';
|
|
39
|
-
return `import 'dotenv
|
|
39
|
+
return `import { config } from 'dotenv';
|
|
40
40
|
import { type Request, type Response } from 'express';
|
|
41
41
|
import { createMcpHandler } from '@modelcontextprotocol/server';
|
|
42
42
|
import { createMcpExpressApp } from '@modelcontextprotocol/express';
|
|
43
43
|
import { toNodeHandler } from '@modelcontextprotocol/node';
|
|
44
44
|
import { getServer } from './server.js';${authImports}
|
|
45
45
|
|
|
46
|
+
// quiet: dotenv >=18 logs a summary line on load; keep startup output clean.
|
|
47
|
+
config({ quiet: true });
|
|
48
|
+
|
|
46
49
|
const allowedHosts = process.env.ALLOWED_HOSTS?.split(',') ?? [];
|
|
47
50
|
|
|
48
51
|
const app = createMcpExpressApp({
|
|
@@ -90,3 +93,9 @@ process.on('SIGINT', async () => {
|
|
|
90
93
|
}
|
|
91
94
|
export { getServerTemplate } from './server.js';
|
|
92
95
|
export { getReadmeTemplate } from './readme.js';
|
|
96
|
+
export { getStoreTemplate } from './store.js';
|
|
97
|
+
export { getToolsTemplate } from './tools.js';
|
|
98
|
+
export { getPromptsTemplate } from './prompts.js';
|
|
99
|
+
export { getResourcesTemplate } from './resources.js';
|
|
100
|
+
export { getStoreTestTemplate } from './store-test.js';
|
|
101
|
+
export { getServerTestTemplate } from './server-test.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function getPromptsTemplate(): string;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
export function getPromptsTemplate() {
|
|
2
|
+
return `import type { GetPromptResult, McpServer } from '@modelcontextprotocol/server';
|
|
3
|
+
import { z } from 'zod';
|
|
4
|
+
|
|
5
|
+
export function registerPrompts(server: McpServer): void {
|
|
6
|
+
server.registerPrompt(
|
|
7
|
+
'summarize-notes',
|
|
8
|
+
{
|
|
9
|
+
description: 'Summarize the notes carrying a given tag',
|
|
10
|
+
argsSchema: z.object({
|
|
11
|
+
tag: z.string().describe('Tag to summarize, e.g. "release"'),
|
|
12
|
+
}),
|
|
13
|
+
},
|
|
14
|
+
async ({ tag }): Promise<GetPromptResult> => {
|
|
15
|
+
return {
|
|
16
|
+
messages: [
|
|
17
|
+
{
|
|
18
|
+
role: 'user',
|
|
19
|
+
content: {
|
|
20
|
+
type: 'text',
|
|
21
|
+
text:
|
|
22
|
+
\`Call list_notes with tag "\${tag}", then summarize those notes \` +
|
|
23
|
+
'in a short paragraph. Mention how many there are.',
|
|
24
|
+
},
|
|
25
|
+
},
|
|
26
|
+
],
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
`;
|
|
32
|
+
}
|
|
@@ -12,6 +12,7 @@ export function getReadmeTemplate(projectName, options) {
|
|
|
12
12
|
build: 'npm run build',
|
|
13
13
|
start: 'npm start',
|
|
14
14
|
inspect: 'npm run inspect',
|
|
15
|
+
test: 'npm test',
|
|
15
16
|
},
|
|
16
17
|
pnpm: {
|
|
17
18
|
install: 'pnpm install',
|
|
@@ -19,6 +20,7 @@ export function getReadmeTemplate(projectName, options) {
|
|
|
19
20
|
build: 'pnpm build',
|
|
20
21
|
start: 'pnpm start',
|
|
21
22
|
inspect: 'pnpm inspect',
|
|
23
|
+
test: 'pnpm test',
|
|
22
24
|
},
|
|
23
25
|
yarn: {
|
|
24
26
|
install: 'yarn',
|
|
@@ -26,6 +28,7 @@ export function getReadmeTemplate(projectName, options) {
|
|
|
26
28
|
build: 'yarn build',
|
|
27
29
|
start: 'yarn start',
|
|
28
30
|
inspect: 'yarn inspect',
|
|
31
|
+
test: 'yarn test',
|
|
29
32
|
},
|
|
30
33
|
}[packageManager];
|
|
31
34
|
const description = withOAuth
|
|
@@ -83,9 +86,13 @@ This server uses OAuth 2.0 with JWT tokens for authentication. It works with any
|
|
|
83
86
|
? `\`\`\`
|
|
84
87
|
${projectName}/
|
|
85
88
|
├── src/
|
|
86
|
-
│ ├── server.ts
|
|
87
|
-
│ ├──
|
|
88
|
-
│
|
|
89
|
+
│ ├── server.ts # Creates the McpServer and registers the primitives
|
|
90
|
+
│ ├── tools.ts # Tool definitions
|
|
91
|
+
│ ├── prompts.ts # Prompt definitions
|
|
92
|
+
│ ├── resources.ts # Resource definitions
|
|
93
|
+
│ ├── notes-store.ts # Example data layer, no MCP imports
|
|
94
|
+
│ ├── index.ts # Express app and MCP HTTP handler setup
|
|
95
|
+
│ └── auth.ts # OAuth configuration and middleware
|
|
89
96
|
├── Dockerfile # Multi-stage Docker build
|
|
90
97
|
├── package.json
|
|
91
98
|
├── tsconfig.json
|
|
@@ -94,8 +101,12 @@ ${projectName}/
|
|
|
94
101
|
: `\`\`\`
|
|
95
102
|
${projectName}/
|
|
96
103
|
├── src/
|
|
97
|
-
│ ├── server.ts
|
|
98
|
-
│
|
|
104
|
+
│ ├── server.ts # Creates the McpServer and registers the primitives
|
|
105
|
+
│ ├── tools.ts # Tool definitions
|
|
106
|
+
│ ├── prompts.ts # Prompt definitions
|
|
107
|
+
│ ├── resources.ts # Resource definitions
|
|
108
|
+
│ ├── notes-store.ts # Example data layer, no MCP imports
|
|
109
|
+
│ └── index.ts # Express app and MCP HTTP handler setup
|
|
99
110
|
├── Dockerfile # Multi-stage Docker build
|
|
100
111
|
├── package.json
|
|
101
112
|
├── tsconfig.json
|
|
@@ -155,20 +166,50 @@ ${oauthSection}
|
|
|
155
166
|
|
|
156
167
|
## Included Examples
|
|
157
168
|
|
|
158
|
-
|
|
169
|
+
A small notes server, included as a worked example of tools an agent can use
|
|
170
|
+
well. Notes are held in memory, so they last until the process restarts.
|
|
159
171
|
|
|
160
|
-
###
|
|
172
|
+
### Tools
|
|
161
173
|
|
|
162
|
-
- **
|
|
174
|
+
- **list_notes** - List notes, newest first, optionally filtered by tag.
|
|
175
|
+
- \`tag\` (optional): Only return notes carrying this tag
|
|
176
|
+
- \`limit\` (optional): Maximum to return, 1-100, defaults to 20
|
|
163
177
|
|
|
164
|
-
|
|
178
|
+
Returns the page plus \`matched\`, \`returned\` and \`truncated\`, so a capped
|
|
179
|
+
result says so and suggests how to narrow it. An unknown tag is reported as
|
|
180
|
+
an error listing the tags that do exist, rather than as an empty list.
|
|
165
181
|
|
|
166
|
-
- **
|
|
167
|
-
- \`
|
|
182
|
+
- **get_note** - Read one note in full by its id.
|
|
183
|
+
- \`id\`: Note id as returned by \`list_notes\` or \`create_note\`
|
|
184
|
+
|
|
185
|
+
- **create_note** - Save a note and return it, including its new id.
|
|
186
|
+
- \`title\`: Non-empty, at most 120 characters
|
|
187
|
+
- \`body\`: Plain text
|
|
188
|
+
- \`tags\` (optional): Tags used to filter in \`list_notes\`
|
|
168
189
|
|
|
169
190
|
### Resources
|
|
170
191
|
|
|
171
|
-
- **
|
|
192
|
+
- **notes://{id}** - A single note as JSON. Listing the resource enumerates
|
|
193
|
+
the saved notes.
|
|
194
|
+
|
|
195
|
+
### Prompts
|
|
196
|
+
|
|
197
|
+
- **summarize-notes** - Summarize the notes carrying a given tag
|
|
198
|
+
|
|
199
|
+
## Tests
|
|
200
|
+
|
|
201
|
+
\`\`\`bash
|
|
202
|
+
${commands.test}
|
|
203
|
+
\`\`\`
|
|
204
|
+
|
|
205
|
+
Two layers: \`src/notes-store.test.ts\` covers the data layer on its own, and
|
|
206
|
+
\`src/server.test.ts\` drives a real MCP client over an in-memory transport and
|
|
207
|
+
asserts on the payload a tool actually returns.
|
|
208
|
+
|
|
209
|
+
These are a worked example, not a quota. Each one pins something a tool's
|
|
210
|
+
description promises but nothing else checks - that a truncated list says so,
|
|
211
|
+
that an error names a next step, that an unknown filter is distinguishable from
|
|
212
|
+
an empty result. When you add a tool, one test in that spirit is enough.
|
|
172
213
|
|
|
173
214
|
## Project Structure
|
|
174
215
|
|
|
@@ -187,9 +228,27 @@ docker run -p 3000:3000 ${projectName}
|
|
|
187
228
|
|
|
188
229
|
## Customization
|
|
189
230
|
|
|
190
|
-
- Add
|
|
231
|
+
- Add tools in \`src/tools.ts\`, prompts in \`src/prompts.ts\`, resources in
|
|
232
|
+
\`src/resources.ts\`. \`src/server.ts\` only wires them together.
|
|
233
|
+
- Replace the example's data layer in \`src/notes-store.ts\`. Keep it free of MCP
|
|
234
|
+
imports so it stays testable on its own, and keep its state at module scope:
|
|
235
|
+
the server factory runs once per request, so anything held on the server
|
|
236
|
+
instance is discarded between calls.
|
|
191
237
|
- Modify the HTTP handler and Express configuration in \`src/index.ts\`${customizationOAuthNote}
|
|
192
238
|
|
|
239
|
+
## Tool design skill
|
|
240
|
+
|
|
241
|
+
Agentailor's [tool-design](https://github.com/agentailor/skills) skill is
|
|
242
|
+
included at \`.agents/skills/tool-design/\`, so a coding agent working here has
|
|
243
|
+
the guidance on hand. Commit it so the whole team gets the same copy.
|
|
244
|
+
|
|
245
|
+
It is the version that shipped with the scaffolding CLI. To check for a newer
|
|
246
|
+
one — your call, not automatic:
|
|
247
|
+
|
|
248
|
+
\`\`\`bash
|
|
249
|
+
npx skills update -p -y
|
|
250
|
+
\`\`\`
|
|
251
|
+
|
|
193
252
|
## Learn More
|
|
194
253
|
|
|
195
254
|
- [Model Context Protocol](https://modelcontextprotocol.io/)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function getResourcesTemplate(): string;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
export function getResourcesTemplate() {
|
|
2
|
+
return `import type { McpServer, ReadResourceResult } from '@modelcontextprotocol/server';
|
|
3
|
+
import { ResourceTemplate } from '@modelcontextprotocol/server';
|
|
4
|
+
import * as notes from './notes-store.js';
|
|
5
|
+
|
|
6
|
+
const MAX_LISTED = 100;
|
|
7
|
+
|
|
8
|
+
export function registerResources(server: McpServer): void {
|
|
9
|
+
server.registerResource(
|
|
10
|
+
'note',
|
|
11
|
+
// \`list\` is a required key on this options object, not an optional one -
|
|
12
|
+
// omitting it fails to compile. Implementing it also lets clients enumerate
|
|
13
|
+
// the notes instead of only reading one by id.
|
|
14
|
+
new ResourceTemplate('notes://{id}', {
|
|
15
|
+
list: async () => ({
|
|
16
|
+
resources: notes.list({ limit: MAX_LISTED }).rows.map((note) => ({
|
|
17
|
+
uri: \`notes://\${note.id}\`,
|
|
18
|
+
name: note.title,
|
|
19
|
+
mimeType: 'application/json',
|
|
20
|
+
})),
|
|
21
|
+
}),
|
|
22
|
+
}),
|
|
23
|
+
{
|
|
24
|
+
title: 'Note',
|
|
25
|
+
description: 'A single saved note, addressed by its id.',
|
|
26
|
+
mimeType: 'application/json',
|
|
27
|
+
},
|
|
28
|
+
async (uri, variables): Promise<ReadResourceResult> => {
|
|
29
|
+
const id = String(variables.id);
|
|
30
|
+
const note = notes.get(id);
|
|
31
|
+
|
|
32
|
+
if (!note) {
|
|
33
|
+
throw new Error(\`No note has id "\${id}"\`);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return {
|
|
37
|
+
contents: [
|
|
38
|
+
{
|
|
39
|
+
uri: uri.href,
|
|
40
|
+
mimeType: 'application/json',
|
|
41
|
+
text: JSON.stringify(note, null, 2),
|
|
42
|
+
},
|
|
43
|
+
],
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
`;
|
|
49
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function getServerTestTemplate(): string;
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
export function getServerTestTemplate() {
|
|
2
|
+
return `import { describe, it, expect, beforeEach } from 'vitest';
|
|
3
|
+
import { InMemoryTransport } from '@modelcontextprotocol/server';
|
|
4
|
+
import { Client } from '@modelcontextprotocol/client';
|
|
5
|
+
import type { CallToolResult } from '@modelcontextprotocol/server';
|
|
6
|
+
import { getServer } from './server.js';
|
|
7
|
+
import * as notes from './notes-store.js';
|
|
8
|
+
|
|
9
|
+
// A worked example of testing a tool, not a target to match. Each test below
|
|
10
|
+
// pins something a tool's description promises but nothing else checks - that
|
|
11
|
+
// is the bar worth copying. Adding a tool is worth one such test; it does not
|
|
12
|
+
// oblige you to cover every branch of your own code here.
|
|
13
|
+
//
|
|
14
|
+
// The assertions run against the payload the tool returns, driven through a
|
|
15
|
+
// real client over an in-memory transport - no HTTP, no subprocess. That is
|
|
16
|
+
// the surface an agent reads, and a tool can be internally correct while still
|
|
17
|
+
// returning something misleading.
|
|
18
|
+
|
|
19
|
+
async function connect(): Promise<Client> {
|
|
20
|
+
const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair();
|
|
21
|
+
const client = new Client({ name: 'test', version: '1.0.0' });
|
|
22
|
+
await Promise.all([getServer().connect(serverTransport), client.connect(clientTransport)]);
|
|
23
|
+
return client;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Tools return JSON as text; parse it rather than asserting on the wrapper. */
|
|
27
|
+
function payload(result: CallToolResult): Record<string, any> {
|
|
28
|
+
const [first] = result.content as Array<{ type: string; text: string }>;
|
|
29
|
+
return JSON.parse(first.text);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function text(result: CallToolResult): string {
|
|
33
|
+
const [first] = result.content as Array<{ type: string; text: string }>;
|
|
34
|
+
return first.text;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
describe('server', () => {
|
|
38
|
+
let client: Client;
|
|
39
|
+
|
|
40
|
+
beforeEach(async () => {
|
|
41
|
+
notes.seedForTests([]);
|
|
42
|
+
client = await connect();
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
// Generic on purpose: this keeps holding once you replace the notes example
|
|
46
|
+
// with your own tools. An undescribed parameter is a common reason an agent
|
|
47
|
+
// calls a tool wrongly.
|
|
48
|
+
it('documents every tool and every parameter', async () => {
|
|
49
|
+
const { tools } = await client.listTools();
|
|
50
|
+
|
|
51
|
+
expect(tools.length).toBeGreaterThan(0);
|
|
52
|
+
|
|
53
|
+
for (const tool of tools) {
|
|
54
|
+
expect(tool.description, tool.name + ' has no description').toBeTruthy();
|
|
55
|
+
|
|
56
|
+
const properties = (tool.inputSchema?.properties ?? {}) as Record<
|
|
57
|
+
string,
|
|
58
|
+
{ description?: string }
|
|
59
|
+
>;
|
|
60
|
+
for (const [name, schema] of Object.entries(properties)) {
|
|
61
|
+
expect(schema.description, tool.name + '.' + name + ' has no description').toBeTruthy();
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it('says so when a result is truncated, and how to narrow it', async () => {
|
|
67
|
+
// The headline contract: a capped list must not look complete.
|
|
68
|
+
for (let i = 0; i < 30; i++) {
|
|
69
|
+
await client.callTool({
|
|
70
|
+
name: 'create_note',
|
|
71
|
+
arguments: { title: 'note ' + i, body: '' },
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const result = payload(
|
|
76
|
+
(await client.callTool({
|
|
77
|
+
name: 'list_notes',
|
|
78
|
+
arguments: { limit: 5 },
|
|
79
|
+
})) as CallToolResult
|
|
80
|
+
);
|
|
81
|
+
|
|
82
|
+
expect(result.returned).toBe(5);
|
|
83
|
+
expect(result.matched).toBe(30);
|
|
84
|
+
expect(result.truncated).toBe(true);
|
|
85
|
+
expect(result.hint).toContain('5 of 30');
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it('does not claim truncation when everything fits', async () => {
|
|
89
|
+
// The inverse case, so "truncated" means something rather than being
|
|
90
|
+
// hardcoded true.
|
|
91
|
+
await client.callTool({ name: 'create_note', arguments: { title: 'only', body: '' } });
|
|
92
|
+
|
|
93
|
+
const result = payload(
|
|
94
|
+
(await client.callTool({ name: 'list_notes', arguments: {} })) as CallToolResult
|
|
95
|
+
);
|
|
96
|
+
|
|
97
|
+
expect(result.truncated).toBe(false);
|
|
98
|
+
expect(result.hint).toBeUndefined();
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it('distinguishes an unknown tag from a tag with no notes', async () => {
|
|
102
|
+
await client.callTool({
|
|
103
|
+
name: 'create_note',
|
|
104
|
+
arguments: { title: 'tagged', body: '', tags: ['release'] },
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
const unknown = (await client.callTool({
|
|
108
|
+
name: 'list_notes',
|
|
109
|
+
arguments: { tag: 'nope' },
|
|
110
|
+
})) as CallToolResult;
|
|
111
|
+
|
|
112
|
+
expect(unknown.isError).toBe(true);
|
|
113
|
+
expect(text(unknown)).toContain('release');
|
|
114
|
+
|
|
115
|
+
const known = payload(
|
|
116
|
+
(await client.callTool({
|
|
117
|
+
name: 'list_notes',
|
|
118
|
+
arguments: { tag: 'release' },
|
|
119
|
+
})) as CallToolResult
|
|
120
|
+
);
|
|
121
|
+
|
|
122
|
+
expect(known.matched).toBe(1);
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
it('returns an actionable error rather than throwing', async () => {
|
|
126
|
+
const result = (await client.callTool({
|
|
127
|
+
name: 'get_note',
|
|
128
|
+
arguments: { id: 'note_zzzzzzzz' },
|
|
129
|
+
})) as CallToolResult;
|
|
130
|
+
|
|
131
|
+
expect(result.isError).toBe(true);
|
|
132
|
+
// The message has to give the agent a next move, not just say "not found".
|
|
133
|
+
expect(text(result)).toContain('list_notes');
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it('returns an id the next call can use', async () => {
|
|
137
|
+
// Chaining without a lookup only works if create hands back the id.
|
|
138
|
+
const created = payload(
|
|
139
|
+
(await client.callTool({
|
|
140
|
+
name: 'create_note',
|
|
141
|
+
arguments: { title: 'Ship v2', body: 'draft' },
|
|
142
|
+
})) as CallToolResult
|
|
143
|
+
);
|
|
144
|
+
|
|
145
|
+
const read = payload(
|
|
146
|
+
(await client.callTool({
|
|
147
|
+
name: 'get_note',
|
|
148
|
+
arguments: { id: created.id },
|
|
149
|
+
})) as CallToolResult
|
|
150
|
+
);
|
|
151
|
+
|
|
152
|
+
expect(read.title).toBe('Ship v2');
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
it('lists the resources it can read', async () => {
|
|
156
|
+
// A ResourceTemplate needs both halves wired up: list enumerates, and the
|
|
157
|
+
// read callback resolves the same uri. Easy to get one without the other.
|
|
158
|
+
const created = payload(
|
|
159
|
+
(await client.callTool({
|
|
160
|
+
name: 'create_note',
|
|
161
|
+
arguments: { title: 'Ship v2', body: 'draft' },
|
|
162
|
+
})) as CallToolResult
|
|
163
|
+
);
|
|
164
|
+
|
|
165
|
+
const uri = 'notes://' + created.id;
|
|
166
|
+
|
|
167
|
+
const { resources } = await client.listResources();
|
|
168
|
+
expect(resources.map((r) => r.uri)).toContain(uri);
|
|
169
|
+
|
|
170
|
+
const read = await client.readResource({ uri });
|
|
171
|
+
expect(JSON.parse(read.contents[0].text as string).title).toBe('Ship v2');
|
|
172
|
+
});
|
|
173
|
+
});
|
|
174
|
+
`;
|
|
175
|
+
}
|
|
@@ -1,79 +1,25 @@
|
|
|
1
1
|
export function getServerTemplate(projectName) {
|
|
2
|
-
return `import
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
} from '@modelcontextprotocol/server';
|
|
7
|
-
import { McpServer } from '@modelcontextprotocol/server';
|
|
8
|
-
import { z } from 'zod';
|
|
2
|
+
return `import { McpServer } from '@modelcontextprotocol/server';
|
|
3
|
+
import { registerTools } from './tools.js';
|
|
4
|
+
import { registerPrompts } from './prompts.js';
|
|
5
|
+
import { registerResources } from './resources.js';
|
|
9
6
|
|
|
7
|
+
/**
|
|
8
|
+
* Builds the MCP server.
|
|
9
|
+
*
|
|
10
|
+
* Called once per request on HTTP, so keep this cheap and keep state out of it:
|
|
11
|
+
* anything stored on the server instance is discarded when the request ends.
|
|
12
|
+
* Durable state belongs at module scope, as in notes-store.ts.
|
|
13
|
+
*/
|
|
10
14
|
export function getServer() {
|
|
11
|
-
// Create an MCP server with implementation details
|
|
12
15
|
const server = new McpServer({
|
|
13
16
|
name: '${projectName}',
|
|
14
17
|
version: '1.0.0',
|
|
15
18
|
});
|
|
16
19
|
|
|
17
|
-
|
|
18
|
-
server
|
|
19
|
-
|
|
20
|
-
{
|
|
21
|
-
description: 'A simple greeting prompt template',
|
|
22
|
-
argsSchema: z.object({
|
|
23
|
-
name: z.string().describe('Name to include in greeting'),
|
|
24
|
-
}),
|
|
25
|
-
},
|
|
26
|
-
async ({ name }): Promise<GetPromptResult> => {
|
|
27
|
-
return {
|
|
28
|
-
messages: [
|
|
29
|
-
{
|
|
30
|
-
role: 'user',
|
|
31
|
-
content: {
|
|
32
|
-
type: 'text',
|
|
33
|
-
text: \`Please greet \${name} in a friendly manner.\`,
|
|
34
|
-
},
|
|
35
|
-
},
|
|
36
|
-
],
|
|
37
|
-
};
|
|
38
|
-
}
|
|
39
|
-
);
|
|
40
|
-
|
|
41
|
-
server.registerTool(
|
|
42
|
-
'greet',
|
|
43
|
-
{
|
|
44
|
-
description: 'Greet a user by name',
|
|
45
|
-
inputSchema: z.object({
|
|
46
|
-
name: z.string().describe('Name of the person to greet'),
|
|
47
|
-
}),
|
|
48
|
-
},
|
|
49
|
-
async ({ name }): Promise<CallToolResult> => {
|
|
50
|
-
return {
|
|
51
|
-
content: [
|
|
52
|
-
{
|
|
53
|
-
type: 'text',
|
|
54
|
-
text: \`Hello, \${name}!\`,
|
|
55
|
-
},
|
|
56
|
-
],
|
|
57
|
-
};
|
|
58
|
-
}
|
|
59
|
-
);
|
|
60
|
-
|
|
61
|
-
// Create a simple resource at a fixed URI
|
|
62
|
-
server.registerResource(
|
|
63
|
-
'greeting-resource',
|
|
64
|
-
'https://example.com/greetings/default',
|
|
65
|
-
{ mimeType: 'text/plain' },
|
|
66
|
-
async (): Promise<ReadResourceResult> => {
|
|
67
|
-
return {
|
|
68
|
-
contents: [
|
|
69
|
-
{
|
|
70
|
-
uri: 'https://example.com/greetings/default',
|
|
71
|
-
text: 'Hello, world!',
|
|
72
|
-
},
|
|
73
|
-
],
|
|
74
|
-
};
|
|
75
|
-
}
|
|
76
|
-
);
|
|
20
|
+
registerTools(server);
|
|
21
|
+
registerPrompts(server);
|
|
22
|
+
registerResources(server);
|
|
77
23
|
|
|
78
24
|
return server;
|
|
79
25
|
}
|