基于FastAPI和Python的JSON RPC服务器
该项目使用Python中的FastAPI、Uvicorn和jsonrpcserver实现了一个简单的JSON-RPC服务器。它暴露了 add 和 multiply 方法。
设置
- 克隆存储库:
git clone
cd json-rpc-server- 使用创建虚拟环境并安装依赖项
uv:
uv venv
.\.venv\Scripts\activate # On Windows
# source .venv/bin/activate # On Linux/macOS
uv pip install fastapi uvicorn jsonrpcserver运行服务器
要使用自动重新加载启动开发服务器,请执行以下操作:
.\.venv\Scripts\activate # On Windows
# source .venv/bin/activate # On Linux/macOS
uv run python -m uvicorn main:app --reload服务器将在以下时间运行 http://127.0.0.1:8000/.
API终点
得到/
一个简单的测试端点,用于确认服务器正在运行。
请求:
# Using curl (Linux/macOS/Git Bash)
curl http://127.0.0.1:8000/
# Using Invoke-RestMethod (PowerShell)
Invoke-RestMethod -Uri "http://127.0.0.1:8000/" -Method Get答复:
{
"message": "FastAPI is running!"
}POST/(JSON-RPC 2.0)
这是JSON-RPC的主要端点 add 和 multiply 方法。
添加方法
请求:
# Using curl
curl -X POST -H "Content-Type: application/json" -d '{"jsonrpc": "2.0", "method": "add", "params": [2, 3], "id": 1}' http://127.0.0.1:8000/
# Using Invoke-RestMethod (PowerShell)
Invoke-RestMethod -Uri "http://127.0.0.1:8000/" -Method Post -Headers @{ "Content-Type" = "application/json" } -Body '{"jsonrpc":"2.0","method":"add","params":[2,3],"id":1}'响应(示例):
{"jsonrpc": "2.0", "result": 5, "id": 1}乘法
请求:
# Using curl
curl -X POST -H "Content-Type: application/json" -d '{"jsonrpc": "2.0", "method": "multiply", "params": [4, 5], "id": 1}' http://127.0.0.1:8000/
# Using Invoke-RestMethod (PowerShell)
Invoke-RestMethod -Uri "http://127.0.0.1:8000/" -Method Post -Headers @{ "Content-Type" = "application/json" } -Body '{"jsonrpc":"2.0","method":"multiply","params":[4,5],"id":1}'响应(示例):
{"jsonrpc": "2.0", "result": 20, "id": 1}信号调用中的乘法方法
curl -X POST -H "Content-Type: application/json" -d '[{"jsonrpc": "2.0", "method": "add", "params": [2, 3], "id": 1},{"jsonrpc": "2.0", "method": "multiply", "params": [2, 30], "id": 2}]' http://127.0.0.1:8000/
Invoke-RestMethod -Uri "http://127.0.0.1:8000/" -Method Post -Headers @{ "Content-Type" = "application/json" } -Body '[{"jsonrpc":"2.0","method":"add","params":[2,3],"id":1},{"jsonrpc":"2.0","method":"multiply","params":[2,30],"id":2}]'安装fastmcp
uv add fastmcp运行MCP检查器
uv run fastmcp dev inspector TestMPCserver.py 运行MCP服务器
uv run fastmcp run TestMPCserver.py连接到cluade桌面
uv run fastmcp install claude-desktop TestMPCserver.py运行MCP服务器
uv run fastmcp run DataprovidersMCPServer.py连接到cluade桌面
uv run fastmcp install claude-desktop DataprovidersMCPServer.pyuv add langchain langchain-openai langchain-mcp-adapters python-dotenv streamlit logging pandas fastmcp python-dotenv jsonrpcserver uvicorn streamlit langchain-ollama fastapi uvicorn serversendevent:app uvicorn Streamable:app流式HTTP演示
该项目包括使用FastAPI的流式HTTP端点演示 StreamingResponse.
启动流服务器
uv run uvicorn Streamable:app --reload服务器将在以下时间运行 http://127.0.0.1:8000/stream 并以1秒的间隔发送10个块。
运行流客户端
python StreamableClient.py客户端演示了使用流式响应的异步和同步方法。
卷曲测试
# Using curl (Linux/macOS/Git Bash)
curl http://127.0.0.1:8000/stream
# Using Invoke-RestMethod (PowerShell)
Invoke-RestMethod -Uri "http://127.0.0.1:8000/stream" -Method GetVS代码调试
A. launch.json 文件提供在 .vscode 目录,以便使用VS代码进行调试。您可以在中设置断点 main.py 并运行“Python:FastAPI”配置。
