Repository navigation
Expand file tree
/
Copy pathtest-mcp-server.ts
More file actions
108 lines (97 loc) · 2.4 KB
/
Copy pathtest-mcp-server.ts
File metadata and controls
108 lines (97 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/env node
/**
* Simple MCP Server for Testing
* Exposes a single "get_weather" tool
*/
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
import {
CallToolRequestSchema,
ListToolsRequestSchema,
Tool,
} from '@modelcontextprotocol/sdk/types.js';
// Create server
const server = new Server(
{
name: 'test-weather-server',
version: '1.0.0',
},
{
capabilities: {
tools: {},
},
},
);
// Define tools
const tools: Tool[] = [
{
name: 'get_weather',
description: 'Get the current weather for a location',
inputSchema: {
type: 'object',
properties: {
location: {
type: 'string',
description: 'City name or zip code',
},
units: {
type: 'string',
enum: ['celsius', 'fahrenheit'],
description: 'Temperature units (default: celsius)',
},
},
required: ['location'],
},
},
];
// Handle tool list requests
server.setRequestHandler(ListToolsRequestSchema, async () => {
console.error('[TEST-MCP] Received list_tools request');
return { tools };
});
// Handle tool calls
server.setRequestHandler(CallToolRequestSchema, async (request) => {
console.error(`[TEST-MCP] Received tool call: ${request.params.name}`);
if (request.params.name === 'get_weather') {
const { location, units = 'celsius' } = request.params.arguments as {
location: string;
units?: string;
};
const weather = {
location: location,
temperature: 22,
condition: 'Sunny',
humidity: 65,
wind_speed: 10,
units: units,
timestamp: new Date().toISOString(),
mcp_server_response: true,
message: `This is a real response from the MCP server for ${location}!`,
};
console.error(`[TEST-MCP] Returning weather for: ${location}`);
return {
content: [
{
type: 'text',
text: JSON.stringify(weather, null, 2),
},
],
};
}
return {
content: [
{
type: 'text',
text: `Unknown tool: ${request.params.name}`,
},
],
isError: true,
};
});
// Start server
async function main() {
const transport = new StdioServerTransport();
await server.connect(transport);
console.error('[TEST-MCP] Server started on stdio');
}
main().catch(console.error);