mcpField
O
OpenAI Integration with MCP

OpenAI Integration with MCP

本地服务
github.com/AashishPalSinghAI 与智能体079 周前更新

关于 OpenAI Integration with MCP

该示例集成让 OpenAI 在回答问题时调用 MCP 服务提供的工具。它连接知识库工具,并在 OpenAI 与服务端之间传递工具调用和结果。

主要功能

  1. 01提供知识库查询工具
  2. 02将 MCP 工具转换为 OpenAI 函数
  3. 03把工具调用转发到 MCP 服务
  4. 04将工具结果返回给 OpenAI

适用场景

  1. 01根据公司政策问答回答问题
  2. 02让 OpenAI 回复关联后端数据
  3. 03将模型集成与后端实现分离

MCP 相关

示例服务端提供 `get_knowledge_base` 工具,客户端通过 SSE 连接,并将 MCP 工具映射为 OpenAI 函数调用。客户端执行工具调用并返回结果,供 OpenAI 使用。

配置

本地服务的配置在各客户端中通用,复制后粘贴到客户端的 MCP 配置文件即可。

暂未收录安装方式

我们还没有获取到这个服务的安装包或远程地址,请按项目 README 中的说明安装。

代码仓库

工具

还没有获取到工具列表

本地服务需要在你的电脑上运行后才能读取工具,维护者暂未提供工具列表。

详细介绍

OpenAI 与 MCP 集成

本节将演示如何将模型上下文协议(MCP)与 OpenAI 的 API 集成,以创建一个能够让 OpenAI 访问和使用 MCP 服务提供的工具的系统。

概述

本示例展示如何:

  1. 创建一个提供知识库工具的 MCP 服务
  2. 将 OpenAI 连接到此 MCP 服务
  3. 让 OpenAI 在回复用户查询时动态使用这些工具

连接方式

本示例使用 sse 传输进行客户端与服务端之间的通信,这意味着:

  • 客户端和服务端运行在不同的进程中
数据流说明
  1. 用户查询:用户向系统发送查询(例如:“我们公司的休假政策是什么?”)
  2. OpenAI API:OpenAI 接收查询以及 MCP 服务提供的可用工具
  3. 工具选择:OpenAI 根据查询决定使用哪些工具
  4. MCP 客户端:客户端接收 OpenAI 的工具调用请求,并将其转发给 MCP 服务
  5. MCP 服务:服务端执行请求的工具(例如,检索知识库数据)
  6. 响应流转:工具结果经由 MCP 客户端返回给 OpenAI
  7. 最终响应:OpenAI 结合工具数据生成最终响应

OpenAI 如何执行工具

OpenAI 的函数调用机制通过以下步骤与 MCP 工具协同工作:

  1. 工具注册:MCP 客户端将 MCP 工具转换为 OpenAI 的函数格式
  2. 工具选择:OpenAI 根据用户查询决定使用哪些工具
  3. 工具执行:MCP 客户端执行所选工具并返回结果
  4. 上下文整合:OpenAI 将工具结果整合到响应中

MCP 的作用

MCP 是 AI 模型与后端系统之间的标准化桥梁:

  • 标准化:MCP 为 AI 模型与工具交互提供一致的接口
  • 抽象:MCP 隐藏后端系统的复杂性
  • 安全性:MCP 让你能够精确控制向 AI 模型公开哪些工具和数据
  • 灵活性:你可以更改后端实现,而无需更改 AI 集成

实现细节

服务端(server.py)

MCP 服务提供一个 get_knowledge_base 工具,用于从 JSON 文件中检索问答对。

客户端(client.py)

客户端:

  1. 连接到 MCP 服务
  2. 将 MCP 工具转换为 OpenAI 的函数格式
  3. 处理 OpenAI 与 MCP 服务之间的通信
  4. 处理工具结果并生成最终响应
知识库(data/kb.json)

包含有关公司政策的问答对,可通过 MCP 服务进行查询。

运行示例

服务端

docker build -t ashujss11/mcp-server .

docker run -p 8050:8050 -d --name mcp-server ashujss11/mcp-server

  1. 确保已安装所需依赖项
  2. 在 .env 文件中设置 OpenAI API 密钥
  3. 运行客户端:python client.py

注意:本示例使用 stdio 传输,因此无需单独运行服务端,客户端会自动启动它。

响应
Initialized SSE client...
Listing tools...

Connected to server with tools: ['get_knowledge_base']

Query: What is our company's vacation policy?

-------START AI Message-------
[
  {
    'role': 'user',
    'content': "What is our company's vacation policy?"
  },
  ChatCompletionMessage(content=None,
  refusal=None,
  role='assistant',
  annotations=[
    
  ],
  audio=None,
  function_call=None,
  tool_calls=[
    ChatCompletionMessageToolCall(id='call_OrCTQBx5Xhsubq2eXge662aU',
    function=Function(arguments='{}',
    name='get_knowledge_base'),
    type='function')
  ]),
  {
    'role': 'tool',
    'tool_call_id': 'call_OrCTQBx5Xhsubq2eXge662aU',
    'content': "Here is the retrieved knowledge base:\n\nQ1: What is our company's vacation policy?\nA1: Full-time employees are entitled to 20 paid vacation days per year. Vacation days can be taken after completing 6 months of employment. Unused vacation days can be carried over to the next year up to a maximum of 5 days. Vacation requests should be submitted at least 2 weeks in advance through the HR portal.\n\nQ2: How do I request a new software license?\nA2: To request a new software license, please submit a ticket through the IT Service Desk portal. Include the software name, version, and business justification. Standard software licenses are typically approved within 2 business days. For specialized software, approval may take up to 5 business days and may require department head approval.\n\nQ3: What is our remote work policy?\nA3: Our company follows a hybrid work model. Employees can work remotely up to 3 days per week. Remote work days must be coordinated with your team and approved by your direct manager. All remote work requires a stable internet connection and a dedicated workspace. Core collaboration hours are 10:00 AM to 3:00 PM EST.\n\nQ4: How do I submit an expense report?\nA4: Expense reports should be submitted through the company's expense management system. Include all receipts, categorize expenses appropriately, and add a brief description for each entry. Reports must be submitted within 30 days of the expense. For expenses over $100, additional documentation may be required. All reports require manager approval.\n\nQ5: What is our process for reporting a security incident?\nA5: If you discover a security incident, immediately contact the Security Team at [email protected] or call the 24/7 security hotline. Do not attempt to investigate or resolve the incident yourself. Document what you observed, including timestamps and affected systems. The Security Team will guide you through the incident response process and may need your assistance for investigation.\n\n"
  }
]
-------END AI Message-------

Response: Our company's vacation policy is as follows:

- Full-time employees are entitled to 20 paid vacation days per year.
- Vacation days can be taken after completing 6 months of employment.
- Unused vacation days can be carried over to the next year, with a maximum carryover of 5 days.
- Vacation requests should be submitted at least 2 weeks in advance through the HR portal.

评论

支持 Markdown

第一次接触 MCP?

从协议是什么,到在客户端里装上第一个服务,这几条回答覆盖了大多数入门问题。

一个开放协议,规定了 AI 应用如何以统一方式连接外部工具与数据源。客户端负责与模型对话,MCP 服务负责提供工具、资源和提示模板,两者通过标准消息通信,同一个服务可以被不同客户端复用。