MCP Server App
# ๐ MCP Server App
## <h1 align="center">Complete MCP Server with LangGraph Agent Integration</h1>
This project demonstrates a comprehensive **Model Context Protocol (MCP) server** with **LangGraph agent integration**, showcasing how to build powerful AI workflows that can interact with custom tools and external APIs.
## ๐ Architecture Overview
```mermaid
graph TD
subgraph "๐ MCP Server App Architecture"
subgraph "Client Layer"
A["๐ค LangGraph Agent<br/>ChatOpenAI + ReAct"]
B["๐ป Interactive Examples<br/>Quick Demo Mode"]
C["๐งช Pytest Test Suite<br/>8 Comprehensive Tests"]
end
subgraph "MCP Integration"
D["๐ MultiServerMCPClient<br/>langchain-mcp-adapters"]
E["๐ก STDIO Transport<br/>Process Communication"]
end
subgraph "Server Layer"
F["โก FastMCP Server<br/>Python MCP Framework"]
G["๐ฒ Dice Roller<br/>Custom Notation Parser"]
H["๐ Web Search<br/>Tavily API Integration"]
I["๐ฑ Social Creator<br/>Unsplash + Quotable APIs"]
M["๐ GitHub Tools<br/>Repository & File Access"]
end
subgraph "External APIs"
J["๐ Tavily Search API<br/>Real-time Web Data"]
K["๐ธ Unsplash API<br/>High-Quality Images"]
L["๐ญ Quotable API<br/>Inspirational Quotes"]
N["๐ GitHub API<br/>Public Repository Data"]
end
end
%% Connections
A --> D
B --> D
C --> F
D --> E
E --> F
F --> G
F --> H
F --> I
F --> M
H --> J
I --> K
I --> L
M --> N
%% Styling
classDef clientStyle fill:#e1f5fe,stroke:#01579b,stroke-width:2px,color:#000
classDef mcpStyle fill:#f3e5f5,stroke:#4a148c,stroke-width:2px,color:#000
classDef serverStyle fill:#e8f5e8,stroke:#1b5e20,stroke-width:2px,color:#000
classDef apiStyle fill:#fff3e0,stroke:#e65100,stroke-width:2px,color:#000
class A,B,C clientStyle
class D,E mcpStyle
class F,G,H,I,M serverStyle
class J,K,L,N apiStyle
```
### Architecture Components
1. **MCPLangGraphAgent**: Main agent class that connects LangGraph to MCP
2. **MultiServerMCPClient**: Handles communication with MCP servers
3. **ReAct Agent**: Uses reasoning and acting to solve problems
4. **StateGraph**: More complex workflow management
5. **MCP Tools**: Your custom tools (dice, web search, content creation)
## ๐ฏ What This Project Enables
Your AI agents can now:
- ๐ฒ **Roll dice** using natural language and D&D notation
- ๐ **Search the web** for real-time information via Tavily API
- ๐จ **Create social media posts** with images and inspirational text
- ๐ผ๏ธ **Find presentation images** for slides and presentations
- ๐ฌ **Generate quote cards** with backgrounds and motivational content
- ๐ **Search GitHub repositories** by query with detailed information
- ๐ **Browse GitHub files** and get repository contents
- ๐ **Read GitHub files** directly from any public repository
## ๐๏ธ Project Structure
```
AIE7-MCP-Session/
โโโ ๐ server/ # MCP Server Components
โ โโโ __init__.py # Server module initialization
โ โโโ main.py # Main MCP server entry point
โ โโโ dice_roller.py # Dice rolling tool implementation
โ โโโ dice_roller_numpy.py # NumPy-based dice roller variant
โ โโโ social_content_creator.py # Social media content creation tools
โ โโโ github_tool.py # GitHub API integration tools
โ
โโโ ๐ client/ # Client Components
โ โโโ __init__.py # Client module initialization
โ โโโ langgraph_agent.py # LangGraph agent implementation
โ
โโโ ๐ tests/ # Test Files
โ โโโ __init__.py # Tests module initialization
โ โโโ test_server.py # Basic MCP server tests
โ โโโ test_mcp_integration.py # Integration tests
โ
โโโ ๐ examples/ # Usage Examples
โ โโโ __init__.py # Examples module initialization
โ โโโ example_langgraph_usage.py # LangGraph usage examples
โ
โโโ ๐ run_server.py # Convenient server entry point
โโโ ๐ run_client.py # Convenient client entry point
โโโ ๐ pyproject.toml # Project dependencies
โโโ ๐ .env.sample # Environment variables template
โโโ ๐ README.md # This documentation
```
## ๐ Quick Start Guide
### Prerequisites
- **Python 3.13+**
- **OpenAI API Key** (required for LangGraph agent)
- **Tavily API Key** (required for web search)
- **Unsplash API Key** (optional - falls back to Lorem Picsum)
- **GitHub Token** (optional - enables private repository access)
### Installation Steps
**1. Clone & Setup**
```bash
git clone <repository-url>
cd AIE7-MCP-Session
cp .env.sample .env
```
**2. Configure API Keys**
Edit `.env` file:
```bash
OPENAI_API_KEY=your_openai_key_here # Required
TAVILY_API_KEY=your_tavily_key_here # Required
UNSPLASH_ACCESS_KEY=your_key_here # Optional
GITHUB_TOKEN=ghp_your_token_here # Optional
```
**3. Install Dependencies**
```bash
uv sync # Recommended
# OR: pip install -r requirements.txt
```
**4. Verify Installation**
```bash
uv run tests/test_mcp_integration.py # Test components
uv run run_client.py # Interactive demo
uv run run_server.py # Test server
```
## ๐ ๏ธ Available Tools
| Tool | Description | Example Usage |
|------|-------------|---------------|
| `web_search` | Search the web via Tavily API | "What's the latest in AI?" |
| `roll_dice` | Roll dice with D&D notation | "Roll 3d6 for character stats" |
| `create_social_post` | Generate social media content | "Create a LinkedIn post about Python" |
| `get_slide_image` | Find presentation images | "Get images for machine learning slides" |
| `create_quote_card` | Generate inspirational quotes | "Make a quote card about innovation" |
## ๐ Usage Examples
### Basic MCP Server Usage
```bash
# Start the MCP server
uv run run_server.py
# In another terminal, test with a simple client
uv run tests/test_server.py
```
### LangGraph Agent Integration
#### Step-by-Step Demo
**1. Start the Interactive Client**
```bash
uv run run_client.py
```
**2. Choose Demo Mode**
```
๐ LangGraph + MCP Integration Examples
Choose what to run:
1. Quick Examples # Automated demonstrations
2. Interactive Mode # Chat with the agent
3. Compare ReAct vs StateGraph # See different agent patterns
Enter choice (1-3): 1
```
**3. Watch the Agent Work**
The agent will automatically demonstrate:
- Rolling dice with natural language
- Searching the web for current information
- Creating social media posts with images
- Finding presentation-ready images
- Generating inspirational quote cards
#### Code Examples
**Basic Usage:**
```python
from client.langgraph_agent import MCPLangGraphAgent
# Create and initialize agent
agent = MCPLangGraphAgent()
await agent.initialize()
# Simple tool usage
result = await agent.chat("Roll 2d20")
# Multi-tool coordination
result = await agent.chat("Roll 2d20 and create a social media post about the results")
```
**Advanced Workflows:**
```python
# Complex multi-tool coordination
result = await agent.chat(
"Search for AI news, create a LinkedIn post about it, and get a slide image"
)
# StateGraph for complex workflows
result = await agent.chat_with_state_graph(
"Research machine learning trends and prepare presentation materials"
)
```
## ๐ก STDIO Transport Communication
Our MCP Server App uses **STDIO transport** for seamless process-to-process communication between the LangGraph agent and FastMCP server.
### ๐ง **1. Server Side - FastMCP Server**
**Files: `server/main.py` and `run_server.py`**
```python
# server/main.py (line 48)
if __name__ == "__main__":
mcp.run(transport="stdio")
# run_server.py (line 20)
main.mcp.run(transport="stdio")
```
- The **FastMCP server** runs with STDIO transport
- This means it communicates via **standard input/output** rather than HTTP
- Perfect for process-to-process communication
### ๐ **2. Client Side - MCP Configuration**
**File: `client/langgraph_agent.py` (lines 55-61)**
```python
self.mcp_config = {
"my-mcp-server": {
"command": "uv",
"args": ["--directory", current_dir, "run", "run_server.py"],
"transport": "stdio", # โ STDIO specified here
}
}
```
- The **MultiServerMCPClient** is configured to use STDIO transport
- It spawns the server process (`uv run run_server.py`)
- Communicates via **stdin/stdout pipes**
### ๐ **3. How STDIO Transport Works**
```
โโโโโโโโโโโโโโโโโโโ STDIO โโโโโโโโโโโโโโโโโโโ
โ LangGraph โโโโโโโโโโโโโโโบโ FastMCP โ
โ Agent โ (pipes) โ Server โ
โ (Client) โ โ (Process) โ
โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ
```
**Process:**
1. **Client** (LangGraph agent) starts the **server process**
2. **STDIO pipes** connect the two processes
3. **JSON-RPC messages** flow over stdin/stdout
4. Server responds to tool calls via the same pipes
### ๐ฏ **Why STDIO?**
- **Process isolation**: Server runs in separate process
- **Clean communication**: No network ports or HTTP servers needed
- **MCP standard**: Official MCP protocol transport method
- **Development friendly**: Easy to debug and test locally
The STDIO transport is the backbone that enables our LangGraph agent to seamlessly communicate with the FastMCP server!
## ๐ง Configuration
### Environment Variables
Create a `.env` file with the following:
```bash
# REQUIRED: OpenAI API Key for LangGraph agent
OPENAI_API_KEY=your_openai_api_key_here
# REQUIRED: Tavily API Key for web search functionality
TAVILY_API_KEY=your_tavily_api_key_here
# OPTIONAL: Unsplash API Key for high-quality images
# If not provided, the system will use Lorem Picsum as fallback
UNSPLASH_ACCESS_KEY=your_unsplash_access_key_here
# OPTIONAL: GitHub Personal Access Token for private repository access
# Create at: https://github.com/settings/tokens
# Scopes needed: repo (for private repos), public_repo (for public repos)
GITHUB_TOKEN=ghp_your_github_personal_access_token_here
# OPTIONAL: LangChain API Key for LangSmith tracing/monitoring
# Only needed if you want to use LangSmith for debugging/monitoring
LANGCHAIN_API_KEY=your_langchain_api_key_here
```
### ๐ GitHub Token Setup (Optional)
To access your **private repositories**, create a GitHub Personal Access Token:
1. **Go to**: https://github.com/settings/tokens
2. **Click**: "Generate new token" โ "Generate new token (classic)"
3. **Set scopes**:
- โ
`repo` (for private repositories)
- โ
`public_repo` (for public repositories)
4. **Copy token** and add to `.env`:
```bash
GITHUB_TOKEN=ghp_your_personal_access_token_here
```
**Without a token**: Only public repositories are accessible
**With a token**: Full access to your private and public repositories
### MCP Configuration for Cursor
To use this MCP server with Cursor, add to your `~/.cursor/mcp.json`:
```json
{
"mcpServers": {
"mcp-server": {
"command": "uv",
"args": ["--directory", "/absolute/path/to/AIE7-MCP-Session", "run", "run_server.py"]
}
}
}
```
## ๐ฎ Interactive Examples & Workflows
### Workflow 1: Content Creation
**Input:** "Create a motivational post about AI and include a dice roll for fun"
**Agent Process:**
1. ๐ฒ Rolls dice using `roll_dice` tool
2. ๐จ Creates social post using `create_social_post` tool
3. ๐ Combines results into engaging content
**Expected Output:**
```
๐ฒ Rolled: 18 (excellent!)
๐ฑ Social Post: "Feeling lucky today! Just rolled an 18 - time to tackle that AI project!
Innovation distinguishes between a leader and a follower. - Steve Jobs
#AI #motivation #lucky18"
```
### Workflow 2: Research & Presentation
**Input:** "Research machine learning trends and prepare a slide"
**Agent Process:**
1. ๐ Searches web using `web_search` tool
2. ๐ผ๏ธ Finds presentation image using `get_slide_image` tool
3. ๐ Summarizes findings for presentation
### Workflow 3: Inspiration Generation
**Input:** "I need motivation for my startup"
**Agent Process:**
1. ๐ฌ Creates quote card using `create_quote_card` tool
2. ๐จ Generates custom inspirational content
3. ๐ฏ Provides actionable motivation
## ๐งช Testing
### Run All Tests with Pytest
```bash
# Run all tests (recommended)
uv run pytest tests/ -v
# Run specific test file
uv run pytest tests/test_server.py -v
uv run pytest tests/test_mcp_integration.py -v
# Run specific test method
uv run pytest tests/test_server.py::TestMCPServer::test_dice_rolling_tool -v
# Run with coverage (if available)
uv run pytest tests/ --cov=server --cov-report=term-missing
```
### Test Coverage
Our comprehensive test suite includes:
- **7 tests** in `test_server.py` - Server functionality, tool registration, and GitHub tools
- **3 tests** in `test_mcp_integration.py` - LangGraph integration tests
- **Total: 10 tests** covering MCP server, tools, GitHub API integration, and client integration
### Manual Testing
```bash
# Test server startup
uv run run_server.py
# Test client integration
uv run run_client.py
# Test specific functionality
uv run examples/example_langgraph_usage.py
# Legacy test execution (backwards compatible)
uv run tests/test_server.py
```
## ๐ ๏ธ Development
### Adding New Server Tools
1. Implement your tool in the `server/` directory
2. Register the tool in `server/main.py` using `@mcp.tool()` decorator
3. Add tests in `tests/`
4. Update documentation
Example:
```python
@mcp.tool()
def my_custom_tool(param: str) -> str:
"""Description of what your tool does"""
return f"Processed: {param}"
```
### Adding New Client Features
1. Implement features in the `client/` directory
2. Add examples in `examples/`
3. Add integration tests in `tests/`
4. Update documentation
### Entry Points
```bash
# Server entry points
uv run server/main.py # Direct server execution
uv run run_server.py # Convenient wrapper
# Client entry points
uv run examples/example_langgraph_usage.py # Direct examples
uv run run_client.py # Convenient wrapper
# Testing entry points
uv run tests/test_mcp_integration.py # Integration tests
pytest tests/ # All tests
```
## ๐ฎ Live Demo Walkthrough
### Quick 5-Minute Demo
**Step 1: Setup (1 minute)**
```bash
# Clone and setup
git clone <repository-url>
cd AIE7-MCP-Session
cp .env.sample .env
# Add your OPENAI_API_KEY and TAVILY_API_KEY to .env
```
**Step 2: Install Dependencies (1 minute)**
```bash
uv sync
```
**Step 3: Test MCP Server (1 minute)**
```bash
# Test server works
uv run run_server.py &
sleep 2
pkill -f run_server.py
echo "โ
Server working!"
```
**Step 4: Run Interactive Demo (2 minutes)**
```bash
uv run run_client.py
# Choose option 1 for automated examples
# Watch the agent coordinate multiple tools
```
### Expected Demo Output
```
๐ฏ Quick LangGraph + MCP Examples
๐น Testing: Roll a d20 for me and tell me what it means for my luck today
๐ Connecting to MCP server...
โ
Connected! Loaded 5 tools
๐ค Agent: You rolled a 19 on a d20! That's excellent luck - great success awaits!
๐น Testing: Search for recent AI news and create a professional social media post
๐ค Agent: Here's a professional LinkedIn post about recent AI developments:
๐ก Reflecting on Recent AI Developments...
[Generated post with image and hashtags]
```
## ๐ฏ Use Cases
### ๐ฒ Gaming & Entertainment
- Generate character stats with dice rolls
- Create social posts about gaming sessions
- Find fantasy-themed images for campaigns
### ๐ Business & Marketing
- Research industry trends
- Create professional social media content
- Generate presentation materials
- Find motivational quotes for teams
### ๐ Education & Training
- Create educational content with images
- Generate quiz questions with dice randomization
- Research topics and create summaries
- Motivational content for students
### ๐ผ Content Creation
- Automated social media campaigns
- Blog post research and imagery
- Quote cards for inspiration
### ๐ Developer Workflow
- Search GitHub repositories for inspiration
- Browse repository files and structure
- Read documentation and code examples
- Analyze open-source project patterns
- Presentation slide generation
## ๐ Troubleshooting
### Common Issues
**Error: "OpenAI API key not found"**
```bash
# Solution: Set your OpenAI API key
echo "OPENAI_API_KEY=your_key_here" >> .env
```
**Error: "MCP server connection failed"**
```bash
# Solution: Check server is working
uv run run_server.py
```
**Error: "No tools loaded"**
```bash
# Solution: Ensure MCP server tools are properly decorated
# Check server/main.py has @mcp.tool() decorators
```
**Cursor MCP Issues:**
1. Restart Cursor after updating `mcp.json`
2. Check Cursor's Developer Tools โ Console for MCP errors
3. Ensure absolute paths in MCP configuration
4. Verify `.env` file has required API keys
## ๐ Advanced Usage
### Custom Tool Integration
```python
# Add your own MCP tools to server/main.py
@mcp.tool()
def my_custom_tool(param: str) -> str:
"""Description of what your tool does"""
return f"Processed: {param}"
# Agent will automatically detect and use new tools
```
### Workflow Customization
```python
# Create custom StateGraph workflows
from langgraph.graph import StateGraph, MessagesState
def custom_workflow():
builder = StateGraph(MessagesState)
# Add your custom nodes and edges
return builder.compile()
```
### Language Model Options
```python
# Different OpenAI models
agent = MCPLangGraphAgent()
agent.llm = ChatOpenAI(
model="gpt-4", # More powerful
# model="gpt-4o-mini", # Cost efficient (default)
temperature=0.7, # Creativity level
)
```
## ๐ค Integration with Other Systems
This setup follows the patterns from the [langchain-mcp-adapters](https://github.com/langchain-ai/langchain-mcp-adapters) repository, making it compatible with:
- **LangGraph API Server**: Deploy as production service
- **Multiple MCP Servers**: Connect to multiple tool sources
- **Streamable HTTP**: Use HTTP transport for web deployment
- **Custom Headers**: Add authentication and custom headers
## ๐ Benefits of This Architecture
### โ
**Clear Separation of Concerns**
- Server logic isolated from client logic
- Tests separated from implementation
- Examples clearly organized
### โ
**Easy Development**
- Add new tools without affecting client code
- Test components independently
- Clear import paths and dependencies
### โ
**Production Ready**
- Server can be deployed independently
- Client can connect to remote servers
- Scalable architecture for multiple clients
### โ
**Maintainable**
- Logical file organization
- Clear entry points for different use cases
- Comprehensive testing structure
## ๐ Next Steps
1. **Explore Examples**: Run `uv run run_client.py` to see all capabilities
2. **Add Custom Tools**: Extend `server/main.py` with your own MCP tools
3. **Create Workflows**: Build complex StateGraph workflows for your use cases
4. **Deploy**: Consider deploying with LangGraph API server for production
5. **Integrate**: Use with Cursor or other MCP-compatible AI assistants
## ๐ License
MIT License - see LICENSE file for details.
## ๐ค Contributing
Contributions are welcome! Please read the contributing guidelines and submit pull requests for any improvements.
---
**Ready to build amazing AI workflows?** Start with `uv run run_client.py` and explore the interactive examples! ๐TDQS
Scored across 10 tools
Each tool targets a distinct purpose: web search, dice rolling, social post creation, slide image generation, quote card creation, and five distinct GitHub operations (search, repo info, file content, file listing, auth status). No two tools have overlapping functionality, and the GitHub prefix clearly separates those tools from the general utilities.
All tools use lowercase snake_case with a clear verb_noun pattern (e.g., web_search, roll_dice, create_social_post) and GitHub tools consistently use a github_ prefix followed by a verb_noun structure (e.g., github_search_repositories, github_get_file_content). The naming is highly predictable and conventional.
With 10 tools, the server sits comfortably in the ideal 3-15 range. Each tool contributes a unique capability, and the mix of 5 general content/search utilities and 5 GitHub utilities feels reasonably scoped for a multi-purpose server.
The GitHub toolset covers read-only operations and search, but lacks common write operations such as creating issues or pull requests, making it incomplete for full repository management. The general tools are a mixed bag without a coherent domain, leaving obvious gaps (e.g., no generic image generation beyond slides/quote cards). However, the existing tools do cover their immediate stated purposes.