|
| 1 | +# Python MCP Demo |
| 2 | + |
| 3 | +A demonstration project showcasing Model Context Protocol (MCP) implementations using FastMCP, with examples of stdio, HTTP transports, and integration with LangChain and Agent Framework. |
| 4 | + |
| 5 | +## Table of Contents |
| 6 | + |
| 7 | +- [Prerequisites](#prerequisites) |
| 8 | +- [Setup](#setup) |
| 9 | +- [Python Scripts](#python-scripts) |
| 10 | +- [MCP Server Configuration](#mcp-server-configuration) |
| 11 | +- [Debugging](#debugging) |
| 12 | +- [License](#license) |
| 13 | + |
| 14 | +## Prerequisites |
| 15 | + |
| 16 | +- Python 3.13 or higher |
| 17 | +- [uv](https://docs.astral.sh/uv/) |
| 18 | +- API access to one of the following: |
| 19 | + - GitHub Models (GitHub token) |
| 20 | + - Azure OpenAI (Azure credentials) |
| 21 | + - Ollama (local installation) |
| 22 | + - OpenAI API (API key) |
| 23 | + |
| 24 | +## Setup |
| 25 | + |
| 26 | +1. Install dependencies using `uv`: |
| 27 | + |
| 28 | +```bash |
| 29 | +uv sync |
| 30 | +``` |
| 31 | + |
| 32 | +2. Copy `.env-sample` to `.env` and configure your environment variables: |
| 33 | + |
| 34 | +```bash |
| 35 | +cp .env-sample .env |
| 36 | +``` |
| 37 | + |
| 38 | +3. Edit `.env` with your API credentials. Choose one of the following providers by setting `API_HOST`: |
| 39 | + - `github` - GitHub Models (requires `GITHUB_TOKEN`) |
| 40 | + - `azure` - Azure OpenAI (requires Azure credentials) |
| 41 | + - `ollama` - Local Ollama instance |
| 42 | + - `openai` - OpenAI API (requires `OPENAI_API_KEY`) |
| 43 | + |
| 44 | +## Python Scripts |
| 45 | + |
| 46 | +Run any script with: `uv run <script_name>` |
| 47 | + |
| 48 | +- **basic_mcp_http.py** - MCP server with HTTP transport on port 8000 |
| 49 | +- **basic_mcp_stdio.py** - MCP server with stdio transport for VS Code integration |
| 50 | +- **langchainv1_mcp_http.py** - LangChain agent with MCP tool integration and temporal context handling |
| 51 | +- **agentframework_mcp_learn.py** - Microsoft Agent Framework integration with MCP |
| 52 | + |
| 53 | +## MCP Server Configuration |
| 54 | + |
| 55 | +### Using with MCP Inspector |
| 56 | + |
| 57 | +The [MCP Inspector](https://github.com/modelcontextprotocol/inspector) is a developer tool for testing and debugging MCP servers. |
| 58 | + |
| 59 | +> **Note:** While HTTP servers can technically work with port forwarding in Codespaces/Dev Containers, the setup for MCP Inspector and debugger attachment is not straightforward. For the best development experience with full debugging capabilities, we recommend running this project locally. |
| 60 | +
|
| 61 | +**For stdio servers:** |
| 62 | + |
| 63 | +```bash |
| 64 | +npx @modelcontextprotocol/inspector uv run basic_mcp_stdio.py |
| 65 | +``` |
| 66 | + |
| 67 | +**For HTTP servers:** |
| 68 | + |
| 69 | +1. Start the HTTP server: |
| 70 | +```bash |
| 71 | +uv run basic_mcp_http.py |
| 72 | +``` |
| 73 | + |
| 74 | +2. In another terminal, run the inspector: |
| 75 | +```bash |
| 76 | +npx @modelcontextprotocol/inspector http://localhost:8000/mcp |
| 77 | +``` |
| 78 | + |
| 79 | +The inspector provides a web interface to: |
| 80 | +- View available tools, resources, and prompts |
| 81 | +- Test tool invocations with custom parameters |
| 82 | +- Inspect server responses and errors |
| 83 | +- Debug server communication |
| 84 | + |
| 85 | +### Using with GitHub Copilot |
| 86 | + |
| 87 | +The `.vscode/mcp.json` file configures MCP servers for GitHub Copilot integration: |
| 88 | + |
| 89 | +**Available Servers:** |
| 90 | + |
| 91 | +- **expenses-mcp**: stdio transport server for production use |
| 92 | +- **expenses-mcp-debug**: stdio server with debugpy on port 5678 |
| 93 | +- **expenses-mcp-http**: HTTP transport server at `http://localhost:8000/mcp` |
| 94 | +- **expenses-mcp-http-debug**: stdio server with debugpy on port 5679 |
| 95 | + |
| 96 | +**Switching Servers:** |
| 97 | + |
| 98 | +Configure which server GitHub Copilot uses by selecting it in the Chat panel selecting the tools icon. |
| 99 | + |
| 100 | +## Debugging |
| 101 | + |
| 102 | +### Debug Configurations |
| 103 | + |
| 104 | +The `.vscode/launch.json` provides four debug configurations: |
| 105 | + |
| 106 | +#### Launch Configurations (Start server with debugging) |
| 107 | + |
| 108 | +1. **Launch MCP HTTP Server (Debug)** |
| 109 | + - Directly starts `basic_mcp_http.py` with debugger attached |
| 110 | + - Best for: Standalone testing and LangChain script debugging |
| 111 | + |
| 112 | +2. **Launch MCP stdio Server (Debug)** |
| 113 | + - Directly starts `basic_mcp_stdio.py` with debugger attached |
| 114 | + - Best for: Testing stdio communication |
| 115 | + |
| 116 | +#### Attach Configurations (Attach to running server) |
| 117 | + |
| 118 | +3. **Attach to MCP Server (stdio)** - Port 5678 |
| 119 | + - Attaches to server started via `expenses-mcp-debug` in `mcp.json` |
| 120 | + - Best for: Debugging during GitHub Copilot Chat usage |
| 121 | + |
| 122 | +4. **Attach to MCP Server (HTTP)** - Port 5679 |
| 123 | + - Attaches to server started via `expenses-mcp-http-debug` in `mcp.json` |
| 124 | + - Best for: Debugging HTTP server during Copilot usage |
| 125 | + |
| 126 | +### Debugging Workflow |
| 127 | + |
| 128 | +#### Option 1: Launch and Debug (Standalone) |
| 129 | + |
| 130 | +Use this approach for debugging with MCP Inspector or LangChain scripts: |
| 131 | + |
| 132 | +1. Set breakpoints in `basic_mcp_http.py` or `basic_mcp_stdio.py` |
| 133 | +2. Press `Cmd+Shift+D` to open Run and Debug |
| 134 | +3. Select "Launch MCP HTTP Server (Debug)" or "Launch MCP stdio Server (Debug)" |
| 135 | +4. Press `F5` or click the green play button |
| 136 | +5. Connect MCP Inspector or run your LangChain script to trigger breakpoints |
| 137 | + - For HTTP: `npx @modelcontextprotocol/inspector http://localhost:8000/mcp` |
| 138 | + - For stdio: `npx @modelcontextprotocol/inspector uv run basic_mcp_stdio.py` (start without debugger first) |
| 139 | + |
| 140 | +#### Option 2: Attach to Running Server (Copilot Integration) |
| 141 | + |
| 142 | +1. Set breakpoints in your MCP server file |
| 143 | +1. Start the debug server via `mcp.json` configuration: |
| 144 | + - Select `expenses-mcp-debug` or `expenses-mcp-http-debug` |
| 145 | +1. Press `Cmd+Shift+D` to open Run and Debug |
| 146 | +1. Select appropriate "Attach to MCP Server" configuration |
| 147 | +1. Press `F5` to attach |
| 148 | +1. Select correct expense mcp server in GitHub Copilot Chat tools |
| 149 | +1. Use GitHub Copilot Chat to trigger the MCP tools |
| 150 | +1. Debugger pauses at breakpoints |
| 151 | + |
| 152 | +## License |
| 153 | + |
| 154 | +MIT |
0 commit comments