-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
130 lines (111 loc) · 3.43 KB
/
Copy pathindex.js
File metadata and controls
130 lines (111 loc) · 3.43 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
125
126
127
128
129
130
#!/usr/bin/env node
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
import {
CallToolRequestSchema,
ListToolsRequestSchema,
InitializeRequestSchema,
} from '@modelcontextprotocol/sdk/types.js';
import dotenv from 'dotenv';
// Load environment variables
dotenv.config();
const TELEGRAM_BOT_TOKEN = process.env.TELEGRAM_BOT_TOKEN;
const TELEGRAM_CHAT_ID = process.env.TELEGRAM_CHAT_ID;
if (!TELEGRAM_BOT_TOKEN || !TELEGRAM_CHAT_ID) {
console.error('TELEGRAM_BOT_TOKEN and TELEGRAM_CHAT_ID must be set in environment variables');
process.exit(1);
}
// Handle Telegram message sending
async function sendTelegramMessage(message) {
const response = await fetch(
`https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage`,
{
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
chat_id: TELEGRAM_CHAT_ID,
text: message,
parse_mode: 'Markdown'
})
}
);
const data = await response.json();
if (response.ok) {
console.log('Telegram message sent successfully');
return { success: true, text: `Message sent! Message ID: ${data.result.message_id}` };
} else {
console.error(`Telegram API error: ${data.description || 'Unknown error'}`);
return { success: false, error: data.description || 'Failed to send message' };
}
}
// Create MCP server
const server = new Server(
{ name: 'telegram', version: '1.0.0' },
{ capabilities: { tools: {} } }
);
// Register list tools handler
server.setRequestHandler(ListToolsRequestSchema, async () => {
return {
tools: [
{
name: 'send_telegram_message',
description: 'Send a message to Telegram via this MCP server',
inputSchema: {
type: 'object',
properties: {
message: {
type: 'string',
description: 'The message text to send'
}
},
required: ['message']
}
}
]
};
});
// Register initialize handler
server.setRequestHandler(InitializeRequestSchema, async (request) => {
const result = {
capabilities: { tools: {} },
protocolVersion: request.params.protocolVersion || '2024-11-05',
serverInfo: { name: 'telegram-mcp', version: '1.0.0' }
};
console.log(`MCP Initialized: protocol=${request.params.protocolVersion}`);
return result;
});
// Register tool call handler
server.setRequestHandler(CallToolRequestSchema, async (request) => {
const params = request.params;
if (params.name !== 'send_telegram_message') {
return {
content: [{ type: 'text', text: `Unknown tool: ${params.name}` }],
isError: true
};
}
const result = await sendTelegramMessage(params.arguments?.message);
return {
content: [
{
type: 'text',
text: result.text || result.error || 'Operation completed'
}
],
isError: !result.success
};
});
// Connect to stdin/stdout for MCP protocol
const transport = new StdioServerTransport();
await server.connect(transport);
// Graceful shutdown
process.on('SIGINT', async () => {
console.log('\nReceived SIGINT, shutting down...');
await server.close();
process.exit(0);
});
process.on('SIGTERM', async () => {
console.log('\nReceived SIGTERM, shutting down...');
await server.close();
process.exit(0);
});
console.log('Telegram MCP Server started successfully!');