Cover image
Try Now
2025-04-14

3 years

Works with Finder

0

Github Watches

0

Github Forks

0

Github Stars

OpenAI Integration with MCP

This section demonstrates how to integrate the Model Context Protocol (MCP) with OpenAI's API to create a system where OpenAI can access and use tools provided by your MCP server.

Overview

This example shows how to:

  1. Create an MCP server that exposes a knowledge base tool
  2. Connect OpenAI to this MCP server
  3. Allow OpenAI to dynamically use the tools when responding to user queries

Connection Methods

This example uses the sse transport for communication between the client and server, which means:

  • The client and server run in the different process

Data Flow Explanation

  1. User Query: The user sends a query to the system (e.g., "What is our company's vacation policy?")
  2. OpenAI API: OpenAI receives the query and available tools from the MCP server
  3. Tool Selection: OpenAI decides which tools to use based on the query
  4. MCP Client: The client receives OpenAI's tool call request and forwards it to the MCP server
  5. MCP Server: The server executes the requested tool (e.g., retrieving knowledge base data)
  6. Response Flow: The tool result flows back through the MCP client to OpenAI
  7. Final Response: OpenAI generates a final response incorporating the tool data

How OpenAI Executes Tools

OpenAI's function calling mechanism works with MCP tools through these steps:

  1. Tool Registration: The MCP client converts MCP tools to OpenAI's function format
  2. Tool Choice: OpenAI decides which tools to use based on the user query
  3. Tool Execution: The MCP client executes the selected tools and returns results
  4. Context Integration: OpenAI incorporates the tool results into its response

The Role of MCP

MCP serves as a standardized bridge between AI models and your backend systems:

  • Standardization: MCP provides a consistent interface for AI models to interact with tools
  • Abstraction: MCP abstracts away the complexity of your backend systems
  • Security: MCP allows you to control exactly what tools and data are exposed to AI models
  • Flexibility: You can change your backend implementation without changing the AI integration

Implementation Details

Server (server.py)

The MCP server exposes a get_knowledge_base tool that retrieves Q&A pairs from a JSON file.

Client (client.py)

The client:

  1. Connects to the MCP server
  2. Converts MCP tools to OpenAI's function format
  3. Handles the communication between OpenAI and the MCP server
  4. Processes tool results and generates final responses

Knowledge Base (data/kb.json)

Contains Q&A pairs about company policies that can be queried through the MCP server.

Running the Example

Server

docker build -t ashujss11/mcp-server .

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

  1. Ensure you have the required dependencies installed
  2. Set up your OpenAI API key in the .env file
  3. Run the client: python client.py

Note: With the stdio transport used in this example, you don't need to run the server separately as the client will automatically start it.

RESPONSE

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 security@company.com 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.

相关推荐

  • av
  • 毫不费力地使用一个命令运行LLM后端,API,前端和服务。

  • 1Panel-dev
  • 🔥1Panel提供了直观的Web接口和MCP服务器,用于在Linux服务器上管理网站,文件,容器,数据库和LLMS。

  • WangRongsheng
  • 🧑‍🚀 llm 资料总结(数据处理、模型训练、模型部署、 o1 模型、mcp 、小语言模型、视觉语言模型)|摘要世界上最好的LLM资源。

  • sigoden
  • 使用普通的bash/javascript/python函数轻松创建LLM工具和代理。

  • hkr04
  • 轻巧的C ++ MCP(模型上下文协议)SDK

  • rulego
  • ⛓️Rulego是一种轻巧,高性能,嵌入式,下一代组件编排规则引擎框架。

  • RockChinQ
  • 😎简单易用、🧩丰富生态 -大模型原生即时通信机器人平台| 适配QQ / 微信(企业微信、个人微信) /飞书 /钉钉 / discord / telegram / slack等平台| 支持chatgpt,deepseek,dify,claude,基于LLM的即时消息机器人平台,支持Discord,Telegram,微信,Lark,Dingtalk,QQ,Slack

  • dmayboroda
  • 带有可配置容器的本地对话抹布

  • modelscope
  • 开始以更轻松的方式开始构建具有LLM授权的多代理应用程序。

  • evilsocket
  • 简单的代理开发套件。

  • ragu6963
  • Reviews

    3 (11)
    Avatar
    user_oJ9VxWmF
    2025-04-24

    As a dedicated user of MCP applications, I must say that MCP-SERVER-KB by AashishPalSingh truly stands out. This product has significantly improved my workflow with its seamless integration and user-friendly interface. The support and welcome information provided make the setup process incredibly smooth. Highly recommend this to anyone looking to enhance their server management experience!

    Avatar
    user_vlkUGBtn
    2025-04-24

    MCP-SERVER-KB by AashishPalSingh is a fantastic tool! It offers comprehensive server management that is efficient and user-friendly. The product's interface is intuitive, making it accessible even for beginners. Highly recommended for anyone needing reliable server solutions!

    Avatar
    user_chC5Vqkh
    2025-04-24

    MCP-SERVER-KB by AashishPalSingh offers an outstanding server management experience. The platform is user-friendly and efficient, making complex tasks simple. The intuitive design, combined with robust functionalities, ensures a seamless operation for all users. Highly recommend for anyone in need of a reliable server management tool!

    Avatar
    user_SIt1qeCi
    2025-04-24

    I have been using MCP-SERVER-KB by AashishPalSingh, and it has significantly improved my server management tasks. The detailed and user-friendly documentation coupled with robust performance makes it a must-have tool in any developer's arsenal. I highly recommend this product to anyone looking to streamline their server operations.

    Avatar
    user_2fnRyvdN
    2025-04-24

    As a loyal user of MCP applications, I am thoroughly impressed with the MCP-SERVER-KB created by AashishPalSingh. This product has significantly streamlined our server management tasks, making our workflow more efficient than ever. Its intuitive interface and comprehensive features are outstanding. Highly recommend it to anyone needing reliable server management solutions!

    Avatar
    user_QBuVHapm
    2025-04-24

    MCP-SERVER-KB by AashishPalSingh is a superb resource for anyone working with My Cloud Platform. This comprehensive guide simplifies complex server configurations and offers clear, practical instructions. Highly recommended for both beginners and experienced users looking to enhance their MCP knowledge.

    Avatar
    user_BmTteM2k
    2025-04-24

    I've been using MCP-SERVER-KB for a while now, and it's absolutely fantastic! Developed by AashishPalSingh, this server solution is robust and efficient. It has vastly improved our workflow and server management. Highly recommended for anyone looking for a reliable server tool.

    Avatar
    user_hJIFZ8nW
    2025-04-24

    I've been using MCP-SERVER-KB by AashishPalSingh, and it's an outstanding tool for managing and organizing server knowledge bases. The interface is intuitive, making it easy to navigate and the setup process is straightforward. The features offered by this application have greatly helped streamline our documentation process and ensure our server management is efficient. Highly recommended for anyone in need of a robust knowledge base solution!

    Avatar
    user_YC03pKe6
    2025-04-24

    I have been using MCP-SERVER-KB, crafted by AashishPalSingh, for a few months now and it's been amazing! The comprehensive knowledge base and seamless integration have transformed our server management experience. Highly recommend it for anyone looking to streamline their processes and boost efficiency!

    Avatar
    user_eLHbVVcE
    2025-04-24

    As a loyal user of MCP applications, I am thoroughly impressed with MCP-SERVER-KB by AashishPalSingh. This product is incredibly reliable and efficient, making server management a breeze. The interface is user-friendly, and the functionality meets all my needs. Highly recommended for anyone looking to streamline their server operations!

    Avatar
    user_N9oDDEGP
    2025-04-24

    As a devoted user of MCP applications, I highly recommend the MCP-SERVER-KB by AashishPalSingh. Its seamless integration and user-friendly design make it an essential tool for efficient server management. The comprehensive features and reliable performance are unparalleled. A must-have for anyone looking to streamline their operations!