Implement MCP 2026-07-28 (#2286)
Co-authored-by: Matt <77928207+mattzcarey@users.noreply.github.com> Co-authored-by: Konstantin Konstantinov <KKonstantinov@users.noreply.github.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
This commit is contained in:
parent
e4227d135c
commit
1823aae891
4 changed files with 69131 additions and 9001 deletions
7
examples/prompts/README.md
Normal file
7
examples/prompts/README.md
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
# prompts
|
||||
|
||||
Register prompts with `McpServer.registerPrompt`; wrap argument schemas with `completable(...)` for autocompletion. The client lists prompts, completes the `language` argument, and renders one with `getPrompt()`.
|
||||
|
||||
```bash
|
||||
pnpm tsx examples/prompts/client.ts
|
||||
```
|
||||
33
examples/prompts/client.ts
Normal file
33
examples/prompts/client.ts
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
/**
|
||||
* Drives the prompts example: list, complete an argument, get a prompt.
|
||||
*/
|
||||
import { check, parseExampleArgs, siblingPath } from '@mcp-examples/shared';
|
||||
import { Client, StreamableHTTPClientTransport } from '@modelcontextprotocol/client';
|
||||
import { StdioClientTransport } from '@modelcontextprotocol/client/stdio';
|
||||
|
||||
const { transport, url, era } = parseExampleArgs();
|
||||
|
||||
const client = new Client(
|
||||
{ name: 'prompts-example-client', version: '1.0.0' },
|
||||
{ versionNegotiation: { mode: era === 'modern' ? 'auto' : 'legacy' } }
|
||||
);
|
||||
|
||||
await (transport === 'stdio'
|
||||
? client.connect(new StdioClientTransport({ command: 'npx', args: ['-y', 'tsx', siblingPath(import.meta.url, 'server.ts')] }))
|
||||
: client.connect(new StreamableHTTPClientTransport(new URL(url))));
|
||||
|
||||
const list = await client.listPrompts();
|
||||
check.ok(list.prompts.some(p => p.name === 'review-code'));
|
||||
|
||||
const completion = await client.complete({
|
||||
ref: { type: 'ref/prompt', name: 'review-code' },
|
||||
argument: { name: 'language', value: 'ty' }
|
||||
});
|
||||
check.ok(completion.completion.values.includes('typescript'));
|
||||
|
||||
const got = await client.getPrompt({ name: 'review-code', arguments: { language: 'rust', code: 'fn main() {}' } });
|
||||
check.equal(got.messages.length, 1);
|
||||
const text = got.messages[0]?.content.type === 'text' ? got.messages[0].content.text : '';
|
||||
check.match(text, /Review this rust code/);
|
||||
|
||||
await client.close();
|
||||
19
examples/prompts/package.json
Normal file
19
examples/prompts/package.json
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
{
|
||||
"name": "@mcp-examples/prompts",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"server": "tsx server.ts",
|
||||
"client": "tsx client.ts"
|
||||
},
|
||||
"dependencies": {
|
||||
"@mcp-examples/shared": "workspace:*",
|
||||
"@modelcontextprotocol/client": "workspace:*",
|
||||
"@modelcontextprotocol/node": "workspace:*",
|
||||
"@modelcontextprotocol/server": "workspace:*",
|
||||
"zod": "catalog:runtimeShared"
|
||||
},
|
||||
"devDependencies": {
|
||||
"tsx": "catalog:devTools"
|
||||
}
|
||||
}
|
||||
54
examples/prompts/server.ts
Normal file
54
examples/prompts/server.ts
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
/**
|
||||
* Prompts primitive + completion.
|
||||
*
|
||||
* Register prompts with `McpServer.registerPrompt`; wrap an arg schema with
|
||||
* `completable(...)` so the client's `complete()` call returns suggestions.
|
||||
* One binary, either transport.
|
||||
*/
|
||||
import { createServer } from 'node:http';
|
||||
|
||||
import { parseExampleArgs } from '@mcp-examples/shared';
|
||||
import { toNodeHandler } from '@modelcontextprotocol/node';
|
||||
import { completable, createMcpHandler, McpServer } from '@modelcontextprotocol/server';
|
||||
import { serveStdio } from '@modelcontextprotocol/server/stdio';
|
||||
import * as z from 'zod/v4';
|
||||
|
||||
const LANGUAGES = ['python', 'typescript', 'rust', 'go'];
|
||||
|
||||
function buildServer(): McpServer {
|
||||
const server = new McpServer({ name: 'prompts-example', version: '1.0.0' });
|
||||
|
||||
server.registerPrompt(
|
||||
'review-code',
|
||||
{
|
||||
title: 'Code review',
|
||||
description: 'Review code for quality and idioms',
|
||||
argsSchema: z.object({
|
||||
language: completable(z.string().describe('Programming language'), value => LANGUAGES.filter(l => l.startsWith(value))),
|
||||
code: z.string().describe('The code to review')
|
||||
})
|
||||
},
|
||||
async ({ language, code }) => ({
|
||||
messages: [
|
||||
{
|
||||
role: 'user',
|
||||
content: { type: 'text', text: `Review this ${language} code for quality and idioms:\n\n${code}` }
|
||||
}
|
||||
]
|
||||
})
|
||||
);
|
||||
|
||||
return server;
|
||||
}
|
||||
|
||||
const { transport, port } = parseExampleArgs();
|
||||
|
||||
if (transport === 'stdio') {
|
||||
void serveStdio(buildServer);
|
||||
console.error('[server] serving over stdio');
|
||||
} else {
|
||||
const handler = createMcpHandler(buildServer);
|
||||
createServer(toNodeHandler(handler)).listen(port, () => {
|
||||
console.error(`[server] listening on http://127.0.0.1:${port}/mcp`);
|
||||
});
|
||||
}
|
||||
Loading…
Reference in a new issue