MCP-SERVER-KB
This document provides a guide on integrating OpenAI with the Model Context Protocol (MCP) to enable OpenAI to access and utilize tools provided by an MCP server.
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:
- Create an MCP server that exposes a knowledge base tool
- Connect OpenAI to this MCP server
- 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
- User Query: The user sends a query to the system (e.g., "What is our company's vacation policy?")
- OpenAI API: OpenAI receives the query and available tools from the MCP server
- Tool Selection: OpenAI decides which tools to use based on the query
- MCP Client: The client receives OpenAI's tool call request and forwards it to the MCP server
- MCP Server: The server executes the requested tool (e.g., retrieving knowledge base data)
- Response Flow: The tool result flows back through the MCP client to OpenAI
- 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:
- Tool Registration: The MCP client converts MCP tools to OpenAI's function format
- Tool Choice: OpenAI decides which tools to use based on the user query
- Tool Execution: The MCP client executes the selected tools and returns results
- 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:
- Connects to the MCP server
- Converts MCP tools to OpenAI's function format
- Handles the communication between OpenAI and the MCP server
- 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
- Ensure you have the required dependencies installed
- Set up your OpenAI API key in the
.env
file - 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.
Related MCP Servers
View all knowledge_and_memory servers →git-mcp
by idosal
GitMCP is a free, open-source, remote Model Context Protocol (MCP) server that transforms GitHub projects into documentation hubs, enabling AI tools to access up-to-date documentation and code.
Knowledge Graph Memory Server
by modelcontextprotocol
A basic implementation of persistent memory using a local knowledge graph, allowing Claude to remember information about the user across chats.
mcpdoc
by langchain-ai
MCP LLMS-TXT Documentation Server provides a structured way to manage and retrieve LLM documentation using the Model Context Protocol.
mindmap-mcp-server
by YuChenSSR
A Model Context Protocol (MCP) server for converting Markdown content to interactive mindmaps.
algorand-mcp
by GoPlausible
This is a Model Context Protocol (MCP) implementation for Algorand blockchain interactions, providing a server package for blockchain interactions and a client package for wallet management and transaction signing.
basic-memory
by basicmachines-co
Basic Memory is a tool that allows users to build a persistent knowledge base through natural conversations with LLMs, storing information in Markdown files.
mcp-obsidian
by MarkusPfundstein
MCP server to interact with Obsidian via the Local REST API community plugin.