这是一个Python解释器MCP服务器,无需API密钥,免费使用,可直接运行Python代码并获取结果。
本MCP服务器提供了运行Python代码、安装包和执行Python文件的工具。它可以轻松地与MCP客户端集成,包括Claude和其他支持MCP协议的大语言模型应用程序。
你可以使用uv来安装MCP Run Python Code Server:
uv pip install mcp-run-python-code
或者使用pip:
pip install mcp-run-python-code
git clone https://github.com/shibing624/mcp-run-python-code.git
cd mcp-run-python-code
pip install -e .
from run_python_code import RunPythonCode
tool = RunPythonCode(base_dir='/tmp/tmp_run_code/')
# 示例1:基本代码执行
result = tool.run_python_code("x = 10\ny = 20\nz = x * y", "z")
print(f"结果: {result}") # 输出: 结果: 200
# 示例2:保存并运行文件
result = tool.save_to_file_and_run(
file_name="calc.py",
code="a = 5\nb = 15\nc = a + b",
variable_to_return="c"
)
print(f"结果: {result}") # 输出: 结果: 20
# 实例3:安装python包
result = tool.pip_install_package("requests")
print(f"结果: {result}")

使用标准输入输出传输运行服务器:
uvx mcp-run-python-code
或者
uv run mcp-run-python-code
或者
python -m mcp-run-python-code
然后,你可以将该服务器与任何支持标准输入输出传输的MCP客户端一起使用。
要将天气MCP服务器添加到Cursor,请使用以下命令添加标准输入输出MCP:
uvx mcp-run-python-code
run_python_code - 执行Python代码并可选择返回变量值。save_to_file_and_run - 将Python代码保存到文件并执行。pip_install_package - 使用pip安装Python包。run_python_file - 运行现有的Python文件并可选择返回变量值。from run_python_code import RunPythonCode
tool = RunPythonCode(base_dir='/tmp/tmp_run_code/')
# Execute simple calculations
code = "result = 2 ** 10"
value = tool.run_python_code(code, "result")
print(value) # Output: 1024
from run_python_code import RunPythonCode
tool = RunPythonCode(base_dir='/tmp/tmp_run_code/')
# Save code to a file and run it
script_code = """
def fibonacci(n):
if n <= 1:
return n
return fibonacci(n-1) + fibonacci(n-2)
result = fibonacci(10)
print(f"Fibonacci(10) = {result}")
"""
result = tool.save_to_file_and_run("fib.py", script_code, "result")
print(result) # Output: 55
from run_python_code import RunPythonCode
tool = RunPythonCode(base_dir='/tmp/tmp_run_code/')
# JSON data processing
code = """
import json
data = {'name': '张三', 'age': 30}
json_str = json.dumps(data, ensure_ascii=False)
"""
result = tool.run_python_code(code, "json_str")
print(result) # Output: {"name": "张三", "age": 30}
本项目遵循 Apache License 2.0,可自由用于商业目的。请在产品描述中包含mcp-run-python-code项目的链接和许可证信息。
我们欢迎对本项目进行贡献!在提交拉取请求之前,请:
tests目录中添加适当的单元测试。python -m pytest以确保所有测试都通过。