Update TypeScript examples to use simplified API
This commit is contained in:
parent
6c610ba46a
commit
16b0cc8ccc
4 changed files with 123 additions and 218 deletions
|
|
@ -210,35 +210,30 @@ Here's a basic example of implementing an MCP server:
|
||||||
|
|
||||||
<Tabs>
|
<Tabs>
|
||||||
<Tab title="TypeScript">
|
<Tab title="TypeScript">
|
||||||
```typescript
|
```typescript
|
||||||
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
|
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
||||||
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
||||||
|
|
||||||
const server = new Server({
|
const server = new McpServer({
|
||||||
name: "example-server",
|
name: "example-server",
|
||||||
version: "1.0.0"
|
version: "1.0.0"
|
||||||
}, {
|
});
|
||||||
capabilities: {
|
|
||||||
resources: {}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Handle requests
|
// Register a resource
|
||||||
server.setRequestHandler(ListResourcesRequestSchema, async () => {
|
server.resource(
|
||||||
return {
|
"example",
|
||||||
resources: [
|
"example://resource",
|
||||||
{
|
async (uri) => ({
|
||||||
uri: "example://resource",
|
contents: [{
|
||||||
name: "Example Resource"
|
uri: uri.href,
|
||||||
}
|
text: "Example resource content"
|
||||||
]
|
}]
|
||||||
};
|
})
|
||||||
});
|
);
|
||||||
|
|
||||||
// Connect transport
|
// Connect transport
|
||||||
const transport = new StdioServerTransport();
|
const transport = new StdioServerTransport();
|
||||||
await server.connect(transport);
|
await server.connect(transport);
|
||||||
```
|
```
|
||||||
</Tab>
|
</Tab>
|
||||||
<Tab title="Python">
|
<Tab title="Python">
|
||||||
```python
|
```python
|
||||||
|
|
|
||||||
|
|
@ -197,98 +197,52 @@ Here's a complete example of implementing prompts in an MCP server:
|
||||||
|
|
||||||
<Tabs>
|
<Tabs>
|
||||||
<Tab title="TypeScript">
|
<Tab title="TypeScript">
|
||||||
```typescript
|
```typescript
|
||||||
import { Server } from "@modelcontextprotocol/sdk/server";
|
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
||||||
import {
|
import { z } from "zod";
|
||||||
ListPromptsRequestSchema,
|
|
||||||
GetPromptRequestSchema
|
|
||||||
} from "@modelcontextprotocol/sdk/types";
|
|
||||||
|
|
||||||
const PROMPTS = {
|
const server = new McpServer({
|
||||||
"git-commit": {
|
name: "example-prompts-server",
|
||||||
name: "git-commit",
|
version: "1.0.0"
|
||||||
description: "Generate a Git commit message",
|
});
|
||||||
arguments: [
|
|
||||||
{
|
|
||||||
name: "changes",
|
|
||||||
description: "Git diff or description of changes",
|
|
||||||
required: true
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"explain-code": {
|
|
||||||
name: "explain-code",
|
|
||||||
description: "Explain how code works",
|
|
||||||
arguments: [
|
|
||||||
{
|
|
||||||
name: "code",
|
|
||||||
description: "Code to explain",
|
|
||||||
required: true
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "language",
|
|
||||||
description: "Programming language",
|
|
||||||
required: false
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const server = new Server({
|
// Register git commit prompt
|
||||||
name: "example-prompts-server",
|
server.prompt(
|
||||||
version: "1.0.0"
|
"git-commit",
|
||||||
}, {
|
"Generate a Git commit message",
|
||||||
capabilities: {
|
{
|
||||||
prompts: {}
|
changes: z.string().describe("Git diff or description of changes")
|
||||||
}
|
},
|
||||||
});
|
({ changes }) => ({
|
||||||
|
messages: [{
|
||||||
|
role: "user",
|
||||||
|
content: {
|
||||||
|
type: "text",
|
||||||
|
text: `Generate a concise but descriptive commit message for these changes:\n\n${changes}`
|
||||||
|
}
|
||||||
|
}]
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
// List available prompts
|
// Register code explanation prompt
|
||||||
server.setRequestHandler(ListPromptsRequestSchema, async () => {
|
server.prompt(
|
||||||
return {
|
"explain-code",
|
||||||
prompts: Object.values(PROMPTS)
|
"Explain how code works",
|
||||||
};
|
{
|
||||||
});
|
code: z.string().describe("Code to explain"),
|
||||||
|
language: z.string().optional().describe("Programming language")
|
||||||
// Get specific prompt
|
},
|
||||||
server.setRequestHandler(GetPromptRequestSchema, async (request) => {
|
({ code, language }) => ({
|
||||||
const prompt = PROMPTS[request.params.name];
|
messages: [{
|
||||||
if (!prompt) {
|
role: "user",
|
||||||
throw new Error(`Prompt not found: ${request.params.name}`);
|
content: {
|
||||||
}
|
type: "text",
|
||||||
|
text: `Explain how this ${language || "Unknown"} code works:\n\n${code}`
|
||||||
if (request.params.name === "git-commit") {
|
}
|
||||||
return {
|
}]
|
||||||
messages: [
|
})
|
||||||
{
|
);
|
||||||
role: "user",
|
```
|
||||||
content: {
|
|
||||||
type: "text",
|
|
||||||
text: `Generate a concise but descriptive commit message for these changes:\n\n${request.params.arguments?.changes}`
|
|
||||||
}
|
|
||||||
}
|
|
||||||
]
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
if (request.params.name === "explain-code") {
|
|
||||||
const language = request.params.arguments?.language || "Unknown";
|
|
||||||
return {
|
|
||||||
messages: [
|
|
||||||
{
|
|
||||||
role: "user",
|
|
||||||
content: {
|
|
||||||
type: "text",
|
|
||||||
text: `Explain how this ${language} code works:\n\n${request.params.arguments?.code}`
|
|
||||||
}
|
|
||||||
}
|
|
||||||
]
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
throw new Error("Prompt implementation not found");
|
|
||||||
});
|
|
||||||
```
|
|
||||||
</Tab>
|
</Tab>
|
||||||
<Tab title="Python">
|
<Tab title="Python">
|
||||||
```python
|
```python
|
||||||
|
|
|
||||||
|
|
@ -40,50 +40,31 @@ Here's an example of implementing a basic tool in an MCP server:
|
||||||
|
|
||||||
<Tabs>
|
<Tabs>
|
||||||
<Tab title="TypeScript">
|
<Tab title="TypeScript">
|
||||||
```typescript
|
```typescript
|
||||||
const server = new Server({
|
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
||||||
name: "example-server",
|
import { z } from "zod";
|
||||||
version: "1.0.0"
|
|
||||||
}, {
|
|
||||||
capabilities: {
|
|
||||||
tools: {}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Define available tools
|
const server = new McpServer({
|
||||||
server.setRequestHandler(ListToolsRequestSchema, async () => {
|
name: "example-server",
|
||||||
return {
|
version: "1.0.0"
|
||||||
tools: [{
|
});
|
||||||
name: "calculate_sum",
|
|
||||||
description: "Add two numbers together",
|
|
||||||
inputSchema: {
|
|
||||||
type: "object",
|
|
||||||
properties: {
|
|
||||||
a: { type: "number" },
|
|
||||||
b: { type: "number" }
|
|
||||||
},
|
|
||||||
required: ["a", "b"]
|
|
||||||
}
|
|
||||||
}]
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
// Handle tool execution
|
// Register a tool
|
||||||
server.setRequestHandler(CallToolRequestSchema, async (request) => {
|
server.tool(
|
||||||
if (request.params.name === "calculate_sum") {
|
"calculate_sum",
|
||||||
const { a, b } = request.params.arguments;
|
"Add two numbers together",
|
||||||
return {
|
{
|
||||||
content: [
|
a: z.number(),
|
||||||
{
|
b: z.number()
|
||||||
type: "text",
|
},
|
||||||
text: String(a + b)
|
async ({ a, b }) => ({
|
||||||
}
|
content: [{
|
||||||
]
|
type: "text",
|
||||||
};
|
text: String(a + b)
|
||||||
}
|
}]
|
||||||
throw new Error("Tool not found");
|
})
|
||||||
});
|
);
|
||||||
```
|
```
|
||||||
</Tab>
|
</Tab>
|
||||||
<Tab title="Python">
|
<Tab title="Python">
|
||||||
```python
|
```python
|
||||||
|
|
|
||||||
|
|
@ -450,64 +450,39 @@ const ForecastArgumentsSchema = z.object({
|
||||||
longitude: z.number().min(-180).max(180),
|
longitude: z.number().min(-180).max(180),
|
||||||
});
|
});
|
||||||
|
|
||||||
// Create server instance
|
```typescript
|
||||||
const server = new Server(
|
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
||||||
|
import { z } from "zod";
|
||||||
|
|
||||||
|
const server = new McpServer({
|
||||||
|
name: "weather",
|
||||||
|
version: "1.0.0"
|
||||||
|
});
|
||||||
|
|
||||||
|
// Register weather alert tool
|
||||||
|
server.tool(
|
||||||
|
"get-alerts",
|
||||||
|
"Get weather alerts for a state",
|
||||||
{
|
{
|
||||||
name: "weather",
|
state: z.string().length(2).describe("Two-letter state code (e.g. CA, NY)")
|
||||||
version: "1.0.0",
|
|
||||||
},
|
},
|
||||||
{
|
async ({ state }) => {
|
||||||
capabilities: {
|
// Tool implementation
|
||||||
tools: {},
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
```
|
|
||||||
|
|
||||||
### Implementing tool listing
|
// Register forecast tool
|
||||||
|
server.tool(
|
||||||
We need to tell clients what tools are available. This `server.setRequestHandler` call will register this list for us:
|
"get-forecast",
|
||||||
|
"Get weather forecast for a location",
|
||||||
```typescript
|
{
|
||||||
// List available tools
|
latitude: z.number().min(-90).max(90).describe("Latitude of the location"),
|
||||||
server.setRequestHandler(ListToolsRequestSchema, async () => {
|
longitude: z.number().min(-180).max(180).describe("Longitude of the location")
|
||||||
return {
|
},
|
||||||
tools: [
|
async ({ latitude, longitude }) => {
|
||||||
{
|
// Tool implementation
|
||||||
name: "get-alerts",
|
}
|
||||||
description: "Get weather alerts for a state",
|
);
|
||||||
inputSchema: {
|
|
||||||
type: "object",
|
|
||||||
properties: {
|
|
||||||
state: {
|
|
||||||
type: "string",
|
|
||||||
description: "Two-letter state code (e.g. CA, NY)",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
required: ["state"],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "get-forecast",
|
|
||||||
description: "Get weather forecast for a location",
|
|
||||||
inputSchema: {
|
|
||||||
type: "object",
|
|
||||||
properties: {
|
|
||||||
latitude: {
|
|
||||||
type: "number",
|
|
||||||
description: "Latitude of the location",
|
|
||||||
},
|
|
||||||
longitude: {
|
|
||||||
type: "number",
|
|
||||||
description: "Longitude of the location",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
required: ["latitude", "longitude"],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
],
|
|
||||||
};
|
|
||||||
});
|
|
||||||
```
|
```
|
||||||
|
|
||||||
This defines our two tools: `get-alerts` and `get-forecast`.
|
This defines our two tools: `get-alerts` and `get-forecast`.
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue