Model Context Protocol (MCP) Fundamentals - Connect LLMs with external tools
sequenceDiagram
participant U as User
participant C as Chatbot
participant O as OpenAI GPT
participant M as MCP Server
participant A as arXiv API
participant S as Local Storage
U->>C: "Search papers about ML"
C->>O: Send query + available tools
O->>C: Returns tool_calls: search_papers()
C->>M: Call search_papers(topic="ML")
M->>A: Fetch papers
A->>M: Return paper data
M->>S: Store papers locally
M->>C: Return paper IDs
C->>O: Send tool results
O->>C: Generate final response
C->>U: "Found 5 papers about ML..."
| File | Description |
|---|---|
mcp_server.py |
MCP server with arXiv tools |
mcp_client.py |
Test client |
mcp_chatbot.py |
OpenAI + MCP chatbot |
# 1. Install dependencies
pip install openai arxiv mcp fastmcp nest-asyncio
# 2. Set API key
export OPENAI_API_KEY="your-api-key"
# 3. Run
python mcp_chatbot.pySearch papers on arXiv by topic
Extract detailed paper information
Your query: Search papers about machine learning
⚡ Calling search_papers with args: {"topic": "machine learning", "max_results": 3}
📄 Result: ['2401.12345', '2401.67890', '2401.54321']
🤖 Assistant: Found 3 papers about machine learning. Want details on any?
Your query: Details of the first one
⚡ Calling extract_info with args: {"paper_id": "2401.12345"}
📄 Result: {"title": "Deep Learning Advances", "authors": ["Smith, J."], ...}
🤖 Assistant: The paper "Deep Learning Advances" by Smith, J. presents...
python mcp_server.pypython mcp_client.pypython mcp_chatbot.pychatbot-arxiv/
├── mcp_server.py # MCP server
├── mcp_client.py # Test client
└── mcp_chatbot.py # Chatbot
- Server: Exposes tools (
@mcp.tool()) - Client: Consumes tools via stdio
- Tools: Functions the LLM can execute
- Protocol: Standard MCP communication
# API key error
export OPENAI_API_KEY="your-key"
# Module error
pip install mcp fastmcp
# File error
cd Course-MCP && python mcp_chatbot.py