-
Notifications
You must be signed in to change notification settings - Fork 772
/
Copy pathmain.ts
208 lines (185 loc) · 5.55 KB
/
main.ts
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/// <reference types="npm:@types/[email protected]" />
import './polyfill.ts'
import http from 'node:http'
import { parseArgs } from '@std/cli/parse-args'
import { SSEServerTransport } from '@modelcontextprotocol/sdk/server/sse.js'
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js'
import { type LoggingLevel, SetLevelRequestSchema } from '@modelcontextprotocol/sdk/types.js'
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'
import { z } from 'zod'
import { asXml, runCode } from './runCode.ts'
const VERSION = '0.0.13'
export async function main() {
const { args } = Deno
if (args.length === 1 && args[0] === 'stdio') {
await runStdio()
} else if (args.length >= 1 && args[0] === 'sse') {
const flags = parseArgs(Deno.args, {
string: ['port'],
default: { port: '3001' },
})
const port = parseInt(flags.port)
runSse(port)
} else if (args.length === 1 && args[0] === 'warmup') {
await warmup()
} else {
console.error(
`\
Invalid arguments.
Usage: deno run -N -R=node_modules -W=node_modules --node-modules-dir=auto jsr:@pydantic/mcp-run-python [stdio|sse|warmup]
options:
--port <port> Port to run the SSE server on (default: 3001)`,
)
Deno.exit(1)
}
}
/*
* Create an MCP server with the `run_python_code` tool registered.
*/
function createServer(): McpServer {
const server = new McpServer(
{
name: 'MCP Run Python',
version: VERSION,
},
{
instructions: 'Call the "run_python_code" tool with the Python code to run.',
capabilities: {
logging: {},
},
},
)
const toolDescription = `Tool to execute Python code and return stdout, stderr, and return value.
The code may be async, and the value on the last line will be returned as the return value.
The code will be executed with Python 3.12.
Dependencies may be defined via PEP 723 script metadata, e.g. to install "pydantic", the script should start
with a comment of the form:
# /// script
# dependencies = ['pydantic']
# ///
print('python code here')
`
let setLogLevel: LoggingLevel = 'emergency'
server.server.setRequestHandler(SetLevelRequestSchema, (request) => {
setLogLevel = request.params.level
return {}
})
server.tool(
'run_python_code',
toolDescription,
{ python_code: z.string().describe('Python code to run') },
async ({ python_code }: { python_code: string }) => {
const logPromises: Promise<void>[] = []
const result = await runCode([{
name: 'main.py',
content: python_code,
active: true,
}], (level, data) => {
if (LogLevels.indexOf(level) >= LogLevels.indexOf(setLogLevel)) {
logPromises.push(server.server.sendLoggingMessage({ level, data }))
}
})
await Promise.all(logPromises)
return {
content: [{ type: 'text', text: asXml(result) }],
}
},
)
return server
}
/*
* Run the MCP server using the SSE transport, e.g. over HTTP.
*/
function runSse(port: number) {
const mcpServer = createServer()
const transports: { [sessionId: string]: SSEServerTransport } = {}
const server = http.createServer(async (req, res) => {
const url = new URL(
req.url ?? '',
`http://${req.headers.host ?? 'unknown'}`,
)
let pathMatch = false
function match(method: string, path: string): boolean {
if (url.pathname === path) {
pathMatch = true
return req.method === method
}
return false
}
function textResponse(status: number, text: string) {
res.setHeader('Content-Type', 'text/plain')
res.statusCode = status
res.end(`${text}\n`)
}
// console.log(`${req.method} ${url}`)
if (match('GET', '/sse')) {
const transport = new SSEServerTransport('/messages', res)
transports[transport.sessionId] = transport
res.on('close', () => {
delete transports[transport.sessionId]
})
await mcpServer.connect(transport)
} else if (match('POST', '/messages')) {
const sessionId = url.searchParams.get('sessionId') ?? ''
const transport = transports[sessionId]
if (transport) {
await transport.handlePostMessage(req, res)
} else {
textResponse(400, `No transport found for sessionId '${sessionId}'`)
}
} else if (pathMatch) {
textResponse(405, 'Method not allowed')
} else {
textResponse(404, 'Page not found')
}
})
server.listen(port, () => {
console.log(
`Running MCP Run Python version ${VERSION} with SSE transport on port ${port}`,
)
})
}
/*
* Run the MCP server using the Stdio transport.
*/
async function runStdio() {
const mcpServer = createServer()
const transport = new StdioServerTransport()
await mcpServer.connect(transport)
}
/*
* Run pyodide to download packages which can otherwise interrupt the server
*/
async function warmup() {
console.error(
`Running warmup script for MCP Run Python version ${VERSION}...`,
)
const code = `
import numpy
a = numpy.array([1, 2, 3])
print('numpy array:', a)
a
`
const result = await runCode([{
name: 'warmup.py',
content: code,
active: true,
}], (level, data) =>
// use warn to avoid recursion since console.log is patched in runCode
console.error(`${level}: ${data}`))
console.log('Tool return value:')
console.log(asXml(result))
console.log('\nwarmup successful 🎉')
}
// list of log levels to use for level comparison
const LogLevels: LoggingLevel[] = [
'debug',
'info',
'notice',
'warning',
'error',
'critical',
'alert',
'emergency',
]
await main()