通过litserve的多endpoint 提供mcp 服务
litserve 支持多endpoint 以及mcp了,这样我们就可以方便的基于多endpoint 提供mcp服务了,以下是一个简单试用
服务定义
- 安装依赖
注意当前litserve 依赖mcp python sdk,但是对于新版本支持有问题(mcp 接口调整,我发了一个pr 已经合并了,但是还没发布),当前版本,如果希望运行正常可以固定mcp 为1.9.4
pip install litserve mcp==1.9.4
- mytools.py
import litserve as ls
from litserve.mcp import MCP
from pydantic import BaseModel
class AddInput(BaseModel):
a: int
b: int
class SubtractInput(BaseModel):
a: int
b: int
class SendEmailInput(BaseModel):
username: str
content: str
class AddApi(ls.LitAPI):
def setup(self, device):
self.model1 = lambda x, y: x + y
def decode_request(self, request: AddInput, **kwargs):
return (request.a, request.b)
def predict(self, x):
a, b = x
result = self.model1(a, b)
print(f"Calculating sum of {a} and {b}: {result}")
return {"output": result}
class SubtractApi(ls.LitAPI):
def setup(self, device):
self.model1 = lambda x, y: x - y
def decode_request(self, request: SubtractInput, **kwargs):
return (request.a, request.b)
def predict(self, x):
a, b = x
result = self.model1(a, b)
print(f"Calculating difference of {a} and {b}: {result}")
return {"output": result}
class SendEmailApi(ls.LitAPI):
def setup(self, device):
pass
def decode_request(self, request: SendEmailInput, **kwargs):
return (request.username, request.content)
def predict(self, x):
username, content = x
print(f"Sending email to {username} with content: {content}")
return {"output": f"Email sent to {username} with content: {content}"}
if __name__ == "__main__":
mcpadd = MCP(description="计算加法",name="get_sum")
mcpsubtract = MCP(description="计算减法",name="get_difference")
mcpsendemail = MCP(description="发送邮件",name="send_email")
api = AddApi(mcp=mcpadd,max_batch_size=1,api_path="/add")
api_subtract = SubtractApi(mcp=mcpsubtract,max_batch_size=1,api_path="/subtract")
api_send_email = SendEmailApi(mcp=mcpsendemail,max_batch_size=1,api_path="/send_email")
server = ls.LitServer([api, api_subtract, api_send_email], accelerator="auto")
server.run(port=8000)
运行效果
- itserve 提供的rest api

- mcp client 的工具显示
配置添加mcp.json 信息
{
"mcpServers": {
"litserve": {
"command": "npx",
"args": ["mcp-remote", "http://localhost:8000/mcp/"]
},
"fastmcp": {
"command": "npx",
"args": ["mcp-remote", "http://localhost:8100/mcp/"]
}
}
}

说明
litserve 的mcp 依赖了python mcp sdk,利用litserve 开发mcp 实际也是一个不错的选择,可以利用不少litserve内置的能力,但是也因为包装的比较深,限制也是会有的,litserve 直接可以兼容openai 协议的模式还是很不错的很值得使用
参考资料
https://lightning.ai/docs/litserve/features/multiple-endpoints
https://lightning.ai/docs/litserve/features/mcp
浙公网安备 33010602011771号