@agentailor/create-mcp-server 0.7.1 → 0.9.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 +34 -11
- 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 +5 -3
- package/dist/cli.test.js +19 -0
- package/dist/deprecations.d.ts +7 -0
- package/dist/deprecations.js +17 -0
- package/dist/deprecations.test.d.ts +1 -0
- package/dist/deprecations.test.js +24 -0
- package/dist/index.js +1 -0
- package/dist/interactive.js +10 -2
- package/dist/project-generator.d.ts +1 -0
- package/dist/project-generator.js +30 -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
|
@@ -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
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function getStoreTestTemplate(): string;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
export function getStoreTestTemplate() {
|
|
2
|
+
return `import { describe, it, expect, beforeEach } from 'vitest';
|
|
3
|
+
import * as notes from './notes-store.js';
|
|
4
|
+
|
|
5
|
+
// The store has no MCP imports, so these run without a server or a transport.
|
|
6
|
+
// Only two tests live here, and both exist because a tool contract depends on
|
|
7
|
+
// them - the rest of the store is ordinary code that needs no demonstration.
|
|
8
|
+
|
|
9
|
+
describe('notes-store', () => {
|
|
10
|
+
beforeEach(() => {
|
|
11
|
+
notes.seedForTests([]);
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
it('reports the full match count, not the size of the page', () => {
|
|
15
|
+
// list_notes can only say "showing 5 of 30" because the store reports both.
|
|
16
|
+
// A store that returned rows.length as the total would make every truncated
|
|
17
|
+
// result look complete - the tool would lie without anything failing.
|
|
18
|
+
for (let i = 0; i < 30; i++) {
|
|
19
|
+
notes.create({ title: 'note ' + i, body: '' });
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const result = notes.list({ limit: 5 });
|
|
23
|
+
|
|
24
|
+
expect(result.rows).toHaveLength(5);
|
|
25
|
+
expect(result.total).toBe(30);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it('distinguishes an unknown tag from a tag with no notes', () => {
|
|
29
|
+
// Both would otherwise return an empty list, and the agent would report
|
|
30
|
+
// "you have no notes" when the truth is "that tag does not exist".
|
|
31
|
+
notes.create({ title: 'tagged', body: '', tags: ['release'] });
|
|
32
|
+
|
|
33
|
+
expect(() => notes.list({ tag: 'nope', limit: 10 })).toThrow(notes.UnknownTagError);
|
|
34
|
+
expect(notes.list({ tag: 'release', limit: 10 }).total).toBe(1);
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
`;
|
|
38
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function getStoreTemplate(): string;
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
export function getStoreTemplate() {
|
|
2
|
+
return `// Domain logic, deliberately free of MCP imports.
|
|
3
|
+
//
|
|
4
|
+
// This module is imported once, so the notes live for the lifetime of the
|
|
5
|
+
// process. That matters: the MCP server factory runs once per request, so
|
|
6
|
+
// anything stored on the server instance would be discarded between calls and
|
|
7
|
+
// notes_create would never be visible to a later notes_list.
|
|
8
|
+
//
|
|
9
|
+
// Replace this with your own data layer. Keep the seam: tools stay thin, the
|
|
10
|
+
// logic stays testable without a transport.
|
|
11
|
+
|
|
12
|
+
export interface Note {
|
|
13
|
+
id: string;
|
|
14
|
+
title: string;
|
|
15
|
+
body: string;
|
|
16
|
+
tags: string[];
|
|
17
|
+
createdAt: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface ListResult {
|
|
21
|
+
/** The page of notes, capped by \`limit\`. */
|
|
22
|
+
rows: Note[];
|
|
23
|
+
/** How many notes matched the filter in total, before the cap was applied. */
|
|
24
|
+
total: number;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export class UnknownTagError extends Error {
|
|
28
|
+
constructor(readonly tag: string) {
|
|
29
|
+
super(\`Unknown tag: \${tag}\`);
|
|
30
|
+
this.name = 'UnknownTagError';
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export const MAX_TITLE_LENGTH = 120;
|
|
35
|
+
|
|
36
|
+
const notes = new Map<string, Note>();
|
|
37
|
+
|
|
38
|
+
let counter = 0;
|
|
39
|
+
|
|
40
|
+
function nextId(): string {
|
|
41
|
+
counter += 1;
|
|
42
|
+
return \`note_\${counter.toString(16).padStart(8, '0')}\`;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Every tag currently in use, so callers can tell a typo from an empty result. */
|
|
46
|
+
export function knownTags(): string[] {
|
|
47
|
+
const tags = new Set<string>();
|
|
48
|
+
for (const note of notes.values()) {
|
|
49
|
+
for (const tag of note.tags) tags.add(tag);
|
|
50
|
+
}
|
|
51
|
+
return [...tags].sort();
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Returns a page of notes plus the unfiltered match count.
|
|
56
|
+
*
|
|
57
|
+
* \`total\` is the number of matches, not the number returned - the caller needs
|
|
58
|
+
* both to tell the agent honestly that a result was truncated.
|
|
59
|
+
*/
|
|
60
|
+
export function list(options: { tag?: string; limit: number }): ListResult {
|
|
61
|
+
const { tag, limit } = options;
|
|
62
|
+
|
|
63
|
+
if (tag !== undefined && !knownTags().includes(tag)) {
|
|
64
|
+
throw new UnknownTagError(tag);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const matches = [...notes.values()]
|
|
68
|
+
.filter((note) => tag === undefined || note.tags.includes(tag))
|
|
69
|
+
.sort((a, b) => b.createdAt.localeCompare(a.createdAt));
|
|
70
|
+
|
|
71
|
+
return { rows: matches.slice(0, limit), total: matches.length };
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function get(id: string): Note | undefined {
|
|
75
|
+
return notes.get(id);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export function create(input: { title: string; body: string; tags?: string[] }): Note {
|
|
79
|
+
const title = input.title.trim();
|
|
80
|
+
|
|
81
|
+
if (title.length === 0) {
|
|
82
|
+
throw new RangeError('title must not be empty');
|
|
83
|
+
}
|
|
84
|
+
if (title.length > MAX_TITLE_LENGTH) {
|
|
85
|
+
throw new RangeError(\`title must be \${MAX_TITLE_LENGTH} characters or fewer\`);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const note: Note = {
|
|
89
|
+
id: nextId(),
|
|
90
|
+
title,
|
|
91
|
+
body: input.body,
|
|
92
|
+
tags: input.tags ?? [],
|
|
93
|
+
createdAt: new Date().toISOString(),
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
notes.set(note.id, note);
|
|
97
|
+
return note;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** Test helper: replaces all notes. Not part of the tool surface. */
|
|
101
|
+
export function seedForTests(seed: Note[]): void {
|
|
102
|
+
notes.clear();
|
|
103
|
+
counter = 0;
|
|
104
|
+
for (const note of seed) notes.set(note.id, note);
|
|
105
|
+
}
|
|
106
|
+
`;
|
|
107
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { describe, it, expect } from 'vitest';
|
|
2
|
-
import { getServerTemplate, getIndexTemplate, getReadmeTemplate } from './index.js';
|
|
2
|
+
import { getServerTemplate, getIndexTemplate, getReadmeTemplate, getStoreTemplate, getToolsTemplate, getPromptsTemplate, getResourcesTemplate, getStoreTestTemplate, getServerTestTemplate, } from './index.js';
|
|
3
3
|
describe('sdk/stateless templates', () => {
|
|
4
4
|
const projectName = 'test-project';
|
|
5
5
|
describe('getServerTemplate', () => {
|
|
@@ -15,30 +15,101 @@ describe('sdk/stateless templates', () => {
|
|
|
15
15
|
const template = getServerTemplate(projectName);
|
|
16
16
|
expect(template).not.toContain('@modelcontextprotocol/sdk');
|
|
17
17
|
});
|
|
18
|
-
it('should
|
|
18
|
+
it('should compose the primitives rather than defining them inline', () => {
|
|
19
19
|
const template = getServerTemplate(projectName);
|
|
20
|
-
expect(template).toContain('
|
|
21
|
-
expect(template).toContain('
|
|
20
|
+
expect(template).toContain("import { registerTools } from './tools.js'");
|
|
21
|
+
expect(template).toContain("import { registerPrompts } from './prompts.js'");
|
|
22
|
+
expect(template).toContain("import { registerResources } from './resources.js'");
|
|
23
|
+
expect(template).toContain('registerTools(server)');
|
|
24
|
+
expect(template).toContain('registerPrompts(server)');
|
|
25
|
+
expect(template).toContain('registerResources(server)');
|
|
22
26
|
});
|
|
23
|
-
it('should
|
|
27
|
+
it('should not register primitives directly', () => {
|
|
24
28
|
const template = getServerTemplate(projectName);
|
|
25
|
-
expect(template).toContain('
|
|
29
|
+
expect(template).not.toContain('server.registerTool(');
|
|
30
|
+
expect(template).not.toContain('server.registerPrompt(');
|
|
31
|
+
expect(template).not.toContain('server.registerResource(');
|
|
32
|
+
});
|
|
33
|
+
it('should not use the deprecated logging subsystem', () => {
|
|
34
|
+
const template = getServerTemplate(projectName);
|
|
35
|
+
expect(template).not.toContain('sendLoggingMessage');
|
|
36
|
+
expect(template).not.toContain('capabilities: { logging: {} }');
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
describe('getToolsTemplate', () => {
|
|
40
|
+
it('should register the notes tools', () => {
|
|
41
|
+
const template = getToolsTemplate();
|
|
26
42
|
expect(template).toContain('registerTool');
|
|
43
|
+
expect(template).toContain("'list_notes'");
|
|
44
|
+
expect(template).toContain("'get_note'");
|
|
45
|
+
expect(template).toContain("'create_note'");
|
|
46
|
+
});
|
|
47
|
+
// Most MCP clients prepend the server name, so a hardcoded prefix would
|
|
48
|
+
// surface as "my-server_notes_list".
|
|
49
|
+
it('should not hardcode a server namespace into tool names', () => {
|
|
50
|
+
const template = getToolsTemplate();
|
|
51
|
+
expect(template).not.toContain("'notes_list'");
|
|
52
|
+
expect(template).not.toContain("'notes_get'");
|
|
53
|
+
expect(template).not.toContain("'notes_create'");
|
|
54
|
+
});
|
|
55
|
+
it('should read its data from the store module', () => {
|
|
56
|
+
const template = getToolsTemplate();
|
|
57
|
+
expect(template).toContain("import * as notes from './notes-store.js'");
|
|
27
58
|
});
|
|
28
59
|
it('should declare schemas as Standard Schema via z.object', () => {
|
|
29
|
-
const template =
|
|
60
|
+
const template = getToolsTemplate();
|
|
30
61
|
expect(template).toContain('inputSchema: z.object(');
|
|
62
|
+
});
|
|
63
|
+
it('should signal truncation rather than silently capping', () => {
|
|
64
|
+
const template = getToolsTemplate();
|
|
65
|
+
expect(template).toContain('truncated');
|
|
66
|
+
expect(template).toContain('matched');
|
|
67
|
+
expect(template).toContain('hint');
|
|
68
|
+
expect(template).toContain('DEFAULT_LIMIT');
|
|
69
|
+
});
|
|
70
|
+
it('should distinguish an unknown tag from a tag with no notes', () => {
|
|
71
|
+
const template = getToolsTemplate();
|
|
72
|
+
expect(template).toContain('UnknownTagError');
|
|
73
|
+
expect(template).toContain('knownTags');
|
|
74
|
+
});
|
|
75
|
+
it('should return actionable errors as isError, not thrown exceptions', () => {
|
|
76
|
+
const template = getToolsTemplate();
|
|
77
|
+
expect(template).toContain('isError: true');
|
|
78
|
+
expect(template).toContain('function toolError');
|
|
79
|
+
});
|
|
80
|
+
it('should point the agent at the sibling tool by its real name', () => {
|
|
81
|
+
const template = getToolsTemplate();
|
|
82
|
+
expect(template).toContain('Call list_notes');
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
describe('getPromptsTemplate', () => {
|
|
86
|
+
it('should register the example prompt', () => {
|
|
87
|
+
const template = getPromptsTemplate();
|
|
88
|
+
expect(template).toContain('registerPrompt');
|
|
89
|
+
expect(template).toContain('summarize-notes');
|
|
31
90
|
expect(template).toContain('argsSchema: z.object(');
|
|
32
91
|
});
|
|
33
|
-
it('should
|
|
34
|
-
const template =
|
|
35
|
-
expect(template).
|
|
36
|
-
expect(template).not.toContain('
|
|
92
|
+
it('should reference the tool by its unprefixed name', () => {
|
|
93
|
+
const template = getPromptsTemplate();
|
|
94
|
+
expect(template).toContain('list_notes');
|
|
95
|
+
expect(template).not.toContain('notes_list');
|
|
37
96
|
});
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
97
|
+
});
|
|
98
|
+
describe('getResourcesTemplate', () => {
|
|
99
|
+
it('should register a resource template for a single note', () => {
|
|
100
|
+
const template = getResourcesTemplate();
|
|
41
101
|
expect(template).toContain('registerResource');
|
|
102
|
+
expect(template).toContain("new ResourceTemplate('notes://{id}'");
|
|
103
|
+
});
|
|
104
|
+
// The v2 type declares `list` as required (not optional), so omitting it
|
|
105
|
+
// fails tsc with TS2741 in the generated project.
|
|
106
|
+
it('should pass list to ResourceTemplate', () => {
|
|
107
|
+
const template = getResourcesTemplate();
|
|
108
|
+
expect(template).toContain('list: async ()');
|
|
109
|
+
});
|
|
110
|
+
it('should read its data from the store module', () => {
|
|
111
|
+
const template = getResourcesTemplate();
|
|
112
|
+
expect(template).toContain("import * as notes from './notes-store.js'");
|
|
42
113
|
});
|
|
43
114
|
});
|
|
44
115
|
describe('getIndexTemplate', () => {
|
|
@@ -138,4 +209,79 @@ describe('sdk/stateless templates', () => {
|
|
|
138
209
|
expect(template).not.toContain('npm run');
|
|
139
210
|
});
|
|
140
211
|
});
|
|
212
|
+
describe('getStoreTemplate', () => {
|
|
213
|
+
it('should not import anything from the MCP SDK', () => {
|
|
214
|
+
const template = getStoreTemplate();
|
|
215
|
+
expect(template).not.toContain('@modelcontextprotocol');
|
|
216
|
+
});
|
|
217
|
+
it('should hold notes at module scope, not per call', () => {
|
|
218
|
+
const template = getStoreTemplate();
|
|
219
|
+
// The MCP server factory runs once per request; state kept on the server
|
|
220
|
+
// would be discarded between calls.
|
|
221
|
+
expect(template).toContain('const notes = new Map<string, Note>()');
|
|
222
|
+
});
|
|
223
|
+
it('should return the unfiltered total alongside the page', () => {
|
|
224
|
+
const template = getStoreTemplate();
|
|
225
|
+
// Callers need both to report truncation honestly.
|
|
226
|
+
expect(template).toContain('rows: Note[]');
|
|
227
|
+
expect(template).toContain('total: number');
|
|
228
|
+
expect(template).toContain('total: matches.length');
|
|
229
|
+
});
|
|
230
|
+
it('should expose the tags in use so callers can detect a typo', () => {
|
|
231
|
+
const template = getStoreTemplate();
|
|
232
|
+
expect(template).toContain('export function knownTags()');
|
|
233
|
+
expect(template).toContain('UnknownTagError');
|
|
234
|
+
});
|
|
235
|
+
it('should validate the title against a documented bound', () => {
|
|
236
|
+
const template = getStoreTemplate();
|
|
237
|
+
expect(template).toContain('MAX_TITLE_LENGTH');
|
|
238
|
+
expect(template).toContain('RangeError');
|
|
239
|
+
});
|
|
240
|
+
it('should provide a seam for seeding in tests', () => {
|
|
241
|
+
const template = getStoreTemplate();
|
|
242
|
+
expect(template).toContain('export function seedForTests(');
|
|
243
|
+
});
|
|
244
|
+
});
|
|
245
|
+
describe('generated test templates', () => {
|
|
246
|
+
it('should exercise the store without a transport', () => {
|
|
247
|
+
const template = getStoreTestTemplate();
|
|
248
|
+
expect(template).toContain("import * as notes from './notes-store.js'");
|
|
249
|
+
expect(template).not.toContain('@modelcontextprotocol/client');
|
|
250
|
+
expect(template).not.toContain('InMemoryTransport');
|
|
251
|
+
});
|
|
252
|
+
it('should pin the store invariants the tools depend on', () => {
|
|
253
|
+
const template = getStoreTestTemplate();
|
|
254
|
+
expect(template).toContain('reports the full match count');
|
|
255
|
+
expect(template).toContain('distinguishes an unknown tag from a tag with no notes');
|
|
256
|
+
expect(template).toContain('seedForTests');
|
|
257
|
+
});
|
|
258
|
+
it('should drive the server through a real client', () => {
|
|
259
|
+
const template = getServerTestTemplate();
|
|
260
|
+
expect(template).toContain("import { Client } from '@modelcontextprotocol/client'");
|
|
261
|
+
expect(template).toContain('InMemoryTransport.createLinkedPair()');
|
|
262
|
+
expect(template).toContain("import { getServer } from './server.js'");
|
|
263
|
+
});
|
|
264
|
+
it('should assert on the parsed payload, not the result wrapper', () => {
|
|
265
|
+
const template = getServerTestTemplate();
|
|
266
|
+
expect(template).toContain('JSON.parse(first.text)');
|
|
267
|
+
expect(template).toContain('result.truncated');
|
|
268
|
+
expect(template).toContain('result.matched');
|
|
269
|
+
});
|
|
270
|
+
it('should cover the contracts that would otherwise fail silently', () => {
|
|
271
|
+
const template = getServerTestTemplate();
|
|
272
|
+
expect(template).toContain('says so when a result is truncated');
|
|
273
|
+
expect(template).toContain('does not claim truncation when everything fits');
|
|
274
|
+
expect(template).toContain('distinguishes an unknown tag from a tag with no notes');
|
|
275
|
+
expect(template).toContain('documents every tool and every parameter');
|
|
276
|
+
});
|
|
277
|
+
// Emitted code with a stray backslash-backtick fails to parse, and the
|
|
278
|
+
// template functions are the only place that can introduce one.
|
|
279
|
+
it('should not emit escaped template-literal syntax', () => {
|
|
280
|
+
const backslash = String.fromCharCode(92);
|
|
281
|
+
for (const template of [getStoreTestTemplate(), getServerTestTemplate()]) {
|
|
282
|
+
expect(template).not.toContain(backslash + '`');
|
|
283
|
+
expect(template).not.toContain(backslash + '$');
|
|
284
|
+
}
|
|
285
|
+
});
|
|
286
|
+
});
|
|
141
287
|
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function getToolsTemplate(): string;
|