当前位置: 首页 > news >正文

agentgateway 简单试用

agentgateway 简单试用

以下是一个简单示例,主要测试多mcp tools 的聚合,集成了基于litserve 的mcp server 以及genai toolbox

安装

目前github 上暂时未系统mac x86 架构的包,可以自己编译

  • 命令
git clone https://github.com/agentgateway/agentgateway.git
make build

配置使用

  • genai toolbox

集成了pg 测试, 具体pg 表以及数据可以参考官方示例文档

sources:my-pg-source:kind: postgreshost: 127.0.0.1port: 5432database: toolbox_dbuser: postgrespassword: dalongdemo
tools:search-hotels-by-name:kind: postgres-sqlsource: my-pg-sourcedescription: 通过名称搜索酒店信息parameters:- name: nametype: stringdescription: 酒店名称。statement: SELECT * FROM hotels WHERE name ILIKE '%' || $1 || '%';search-hotels-by-location:kind: postgres-sqlsource: my-pg-sourcedescription: 通过位置搜索酒店信息parameters:- name: locationtype: stringdescription: 酒店位置。statement: SELECT * FROM hotels WHERE location ILIKE '%' || $1 || '%';book-hotel:kind: postgres-sqlsource: my-pg-sourcedescription: >-Book a hotel by its ID. If the hotel is successfully booked, returns a NULL, raises an error if not.parameters:- name: hotel_idtype: stringdescription: The ID of the hotel to book.statement: UPDATE hotels SET booked = B'1' WHERE id = $1;update-hotel:kind: postgres-sqlsource: my-pg-sourcedescription: >-Update a hotel's check-in and check-out dates by its ID. Returns a messageindicating  whether the hotel was successfully updated or not.parameters:- name: hotel_idtype: stringdescription: The ID of the hotel to update.- name: checkin_datetype: stringdescription: The new check-in date of the hotel.- name: checkout_datetype: stringdescription: The new check-out date of the hotel.statement: >-UPDATE hotels SET checkin_date = CAST($2 as date), checkout_date = CAST($3as date) WHERE id = $1;cancel-hotel:kind: postgres-sqlsource: my-pg-sourcedescription: Cancel a hotel by its ID.parameters:- name: hotel_idtype: stringdescription: The ID of the hotel to cancel.statement: UPDATE hotels SET booked = B'0' WHERE id = $1;
toolsets:my-toolset:- search-hotels-by-name- search-hotels-by-location- book-hotel- update-hotel- cancel-hotel

启动

./toolbox --tools-file tools.yaml
  • litserve mcp server
import litserve as ls
from litserve.mcp import MCP
from pydantic import BaseModel
from litserve.specs.openai import ChatMessage
from litserve.specs import OpenAIEmbeddingSpec,OpenAISpecclass AddInput(BaseModel):a: intb: intclass SubtractInput(BaseModel):a: intb: intclass SendEmailInput(BaseModel):username: strcontent: strclass AddApi(ls.LitAPI):def setup(self, device):self.model1 = lambda x, y: x + ydef decode_request(self, request: AddInput, **kwargs):return (request.a, request.b)def predict(self, x):a, b = xresult = 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 - ydef decode_request(self, request: SubtractInput, **kwargs):return (request.a, request.b)def predict(self, x):a, b = xresult = self.model1(a, b)print(f"Calculating difference of {a} and {b}: {result}")return {"output": result}
class SendEmailApi(ls.LitAPI):def setup(self, device):passdef decode_request(self, request: SendEmailInput, **kwargs):return (request.username, request.content)def predict(self, x):username, content = xprint(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)
  • agentgateway 集成配置

config.yaml

binds:
- port: 3000listeners:- routes:- backends:- mcp:name: defaulttargets:- name: demomcp:host: localhostport: 8000path: /mcp/- name: everythingmcp:host: 127.0.0.1port: 5000path: /mcp/my-toolset/
  • 启动
./agentgateway -f config.yaml
  • 效果

说明

agentgateway 最近一个版本的调整了不少配置,如果要使用最好解决源码学习,同时目前看ui 部分是有一些问题的,整体来说还是很不错的,目前来说bug还是不少的,实际上genai toolbox 的toolsets 也是一个挺有意思的设计

参考资料

crates/agentgateway/src/types/agent.rs

https://github.com/agentgateway/agentgateway

https://agentgateway.dev/

https://github.com/agentgateway/agentgateway/releases/tag/v0.5.2

https://github.com/cloudflare/pingora