-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
executable file
·124 lines (110 loc) · 3.03 KB
/
server.js
File metadata and controls
executable file
·124 lines (110 loc) · 3.03 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/env node
import 'dotenv/config';
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import {
CallToolRequestSchema,
ListResourcesRequestSchema,
ListToolsRequestSchema,
ReadResourceRequestSchema
} from "@modelcontextprotocol/sdk/types.js";
import { TOOLS } from './tools/index.js';
import fs from 'fs';
// Check for API key in environment variables
const VERBWIRE_API_KEY = process.env.VERBWIRE_API_KEY;
if (!VERBWIRE_API_KEY) {
throw new Error("VERBWIRE_API_KEY not found in environment variables. Please add it to your .env file.");
}
// Simple logging function
const log = (message) => {
console.error(`[${new Date().toISOString()}] ${message}`);
};
// Create the MCP server
const server = new Server(
{
name: "verbwire-mcp-server",
version: "1.0.0",
},
{
capabilities: {
resources: {},
tools: {},
},
}
);
// Handle tool calls
async function handleToolCall(name, args) {
log(`Tool call: ${name}`);
try {
const tool = TOOLS.find(t => t.name === name);
if (!tool) {
throw new Error(`Tool not found: ${name}`);
}
const result = await tool.handler(args, VERBWIRE_API_KEY);
return {
content: [{
type: "text",
text: result
}],
isError: false
};
} catch (error) {
log(`Error in tool call: ${error.message}`);
return {
content: [{
type: "text",
text: `Error: ${error.message || 'Unknown error'}`
}],
isError: true
};
}
}
// Set up request handlers
server.setRequestHandler(ListToolsRequestSchema, async () => ({
tools: TOOLS
}));
server.setRequestHandler(CallToolRequestSchema, async (request) =>
handleToolCall(request.params.name, request.params.arguments ?? {})
);
server.setRequestHandler(ReadResourceRequestSchema, async (request) => {
const { uri } = request.params;
log(`Resource request: ${uri}`);
// Simple resource handling for file uploads
if (uri.startsWith('file://')) {
const filePath = uri.slice(7);
if (fs.existsSync(filePath)) {
return {
content: fs.readFileSync(filePath).toString('base64'),
contentType: 'application/octet-stream',
encoding: 'base64'
};
} else {
log(`File not found: ${filePath}`);
throw new Error(`File not found: ${filePath}`);
}
}
log(`Resource not found: ${uri}`);
throw new Error(`Resource not found: ${uri}`);
});
// Run the server
async function run() {
// Connect to standard input/output for communication
const transport = new StdioServerTransport();
await server.connect(transport);
log('Verbwire MCP server is running...');
// Handle shutdown signals
process.on('SIGINT', () => {
log('Shutting down...');
server.close();
process.exit(0);
});
process.on('SIGTERM', () => {
log('Shutting down...');
server.close();
process.exit(0);
});
}
run().catch(error => {
log(`Error starting server: ${error.message}`);
process.exit(1);
});