代理工具管理SDK

OpenAI代理工具管理SDK,支持动态工具过滤、提示生成和工具缓存,适用于自动化代理开发。

  • by ccaiy9
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 ccaiy9/mcp-server-demo

使用说明

1. openai agent sdk MCP使用介绍

https://openai.github.io/openai-agents-python/mcp/

可以使用MCPServerStdio、MCPServerSse和MCPServerStreamableHttp类来连接到这些服务器。

1.1 使用 MCP 服务器

MCP 服务器可以添加到 Agent 中:

agent=Agent(
    name="Assistant",
    instructions="Use the tools to achieve the task",
    mcp_servers=[mcp_server_1, mcp_server_2]
)

每次启动Agent 时,框架都会连接MCP服务器调用一次list_tools(),使得 LLM 能动态感知 MCP 服务器的工具;当 LLM 需要从从 MCP 服务器调用工具时,SDK 也会调用call_tool()该服务器。 也就是说全自动!!

1.2工具过滤

可以通过在 MCP 服务器上配置工具过滤器来过滤代理可用的工具。SDK 支持静态和动态工具过滤。

  1. 静态工具过滤
from agents.mcp import create_static_tool_filter

# Only expose specific tools from this server
server = MCPServerStdio(
    params={
        "command": "npx",
        "args": ["-y", "@modelcontextprotocol/server-filesystem", samples_dir],
    },
    tool_filter=create_static_tool_filter(
        allowed_tool_names=["read_file", "write_file"]
    )
)

# Exclude specific tools from this server
server = MCPServerStdio(
    params={
        "command": "npx", 
        "args": ["-y", "@modelcontextprotocol/server-filesystem", samples_dir],
    },
    tool_filter=create_static_tool_filter(
        blocked_tool_names=["delete_file"]
    )
)

当同时配置allowed_tool_names和blocked_tool_names时: 例如,如果您配置allowed_tool_names=["read_file", "write_file", "delete_file"]和blocked_tool_names=["delete_file"],则只有read_file和write_file工具可用。

  1. 动态工具过滤 ... 有点复杂暂时没必要

1.3 Prompts

MCP 服务器还可以提供用于动态生成代理指令的prompts。这允许您创建可重复使用的指令模板,并可使用参数进行自定义。

  1. 使用提示 支持提示的 MCP 服务器提供两种主要方法:
  • list_prompts():列出服务器上所有可用的提示
  • get_prompt(name, arguments):获取带有可选参数的特定提示
# List available prompts
prompts_result = await server.list_prompts()
for prompt in prompts_result.prompts:
    print(f"Prompt: {prompt.name} - {prompt.description}")

# Get a specific prompt with parameters
prompt_result = await server.get_prompt(
    "generate_code_review_instructions",
    {"focus": "security vulnerabilities", "language": "python"}
)
instructions = prompt_result.messages[0].content.text

# Use the prompt-generated instructions with an Agent
agent = Agent(
    name="Code Reviewer",
    instructions=instructions,  # Instructions from MCP prompt
    mcp_servers=[server]
)

1.4 缓存

每次运行agent时,它都会调用MCP 服务器端list_tools()。这可能会造成延迟,尤其是在服务器是远程服务器的情况下。要自动缓存工具列表,您可以将、和传递cache_tools_list=True给。仅当您确定工具列表不会更改时才应这样做。 如果你想使缓存无效,你可以调用invalidate_tools_cache()服务器。

1.5 框架是怎么自动调用call_tools的?

class AgentRunner:run
	AgentRunner:_get_all_tools
		Agent:get_all_tools
			Agent:get_mcp_tools
				MCPUtil:get_all_function_tools
					MCPUtil:get_function_tools
						- tools = _MCPServerWithClientSession:list_tools   # 这里的tools包含名称、描述
						- MCPUtil:to_function_tool(tools)
								MCPUtil:invoke_mcp_tool
								_MCPServerWithClientSession:call_tool

如果不使用框架,mcp调用流程该怎么写?

  1. 需要自己list_tools,将接口名、描述、arguments写到prompt
  2. 获取llm的response,拿到tool_name、arguments调用call_tool
  3. 且经过多轮获取llm的response,调用call_tool,直到完成
  4. 怎么定义完成?这个可以在prompt中去说明完成后的返回