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: postgres
    host: 127.0.0.1
    port: 5432
    database: toolbox_db
    user: postgres
    password: dalongdemo
tools:
  search-hotels-by-name:
    kind: postgres-sql
    source: my-pg-source
    description: 通过名称搜索酒店信息
    parameters:
      - name: name
        type: string
        description: 酒店名称。
    statement: SELECT * FROM hotels WHERE name ILIKE '%' || $1 || '%';
  search-hotels-by-location:
    kind: postgres-sql
    source: my-pg-source
    description: 通过位置搜索酒店信息
    parameters:
      - name: location
        type: string
        description: 酒店位置。
    statement: SELECT * FROM hotels WHERE location ILIKE '%' || $1 || '%';
  book-hotel:
    kind: postgres-sql
    source: my-pg-source
    description: >-
       Book a hotel by its ID. If the hotel is successfully booked, returns a NULL, raises an error if not.
    parameters:
      - name: hotel_id
        type: string
        description: The ID of the hotel to book.
    statement: UPDATE hotels SET booked = B'1' WHERE id = $1;
  update-hotel:
    kind: postgres-sql
    source: my-pg-source
    description: >-
      Update a hotel's check-in and check-out dates by its ID. Returns a message
      indicating  whether the hotel was successfully updated or not.
    parameters:
      - name: hotel_id
        type: string
        description: The ID of the hotel to update.
      - name: checkin_date
        type: string
        description: The new check-in date of the hotel.
      - name: checkout_date
        type: string
        description: The new check-out date of the hotel.
    statement: >-
      UPDATE hotels SET checkin_date = CAST($2 as date), checkout_date = CAST($3
      as date) WHERE id = $1;
  cancel-hotel:
    kind: postgres-sql
    source: my-pg-source
    description: Cancel a hotel by its ID.
    parameters:
      - name: hotel_id
        type: string
        description: 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,OpenAISpec

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)
  • agentgateway 集成配置

config.yaml

binds:
- port: 3000
  listeners:
  - routes:
    - backends:
      - mcp:
          name: default
          targets:
          - name: demo
            mcp:
              host: localhost
              port: 8000
              path: /mcp/
          - name: everything
            mcp:
              host: 127.0.0.1
              port: 5000
              path: /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

posted on 2025-09-19 08:00  荣锋亮  阅读(8)  评论(0)    收藏  举报

导航