|
1 | | -# Python MCP Demos |
| 1 | +# Python MCP Demo |
2 | 2 |
|
3 | | -This repository implements a **minimal MCP expense tracker**. |
4 | | - |
5 | | -The Model Context Protocol (MCP) is an open standard that enables LLMs to connect to external data sources and tools. |
| 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. |
6 | 4 |
|
7 | 5 | ## Table of Contents |
8 | 6 |
|
9 | | -- [Getting Started](#getting-started) |
10 | | - - [Environment Setup](#environment-setup) |
11 | | - - [Run the MCP Server in VS Code](#run-the-mcp-server-in-vs-code) |
12 | | - - [GitHub Copilot Chat Integration](#github-copilot-chat-integration) |
13 | | - - [MCP Inspector](#mcp-inspector) |
| 7 | +- [Prerequisites](#prerequisites) |
| 8 | +- [Setup](#setup) |
| 9 | +- [Python Scripts](#python-scripts) |
| 10 | +- [MCP Server Configuration](#mcp-server-configuration) |
14 | 11 | - [Debugging](#debugging) |
15 | | - - [Debugging with VS Code and debugpy](#debugging-with-vs-code-and-debugpy) |
16 | | - - [Testing with MCP Inspector](#testing-with-mcp-inspector) |
17 | | -- [Contributing](#contributing) |
18 | | - |
19 | | - |
20 | | -## Environment Setup |
21 | | - |
22 | | -#### 1. GitHub Codespaces |
23 | | - |
24 | | -1. Click the **Code** button |
25 | | -2. Select the **Codespaces** tab |
26 | | -3. Click **Create codespace on main** |
27 | | -4. Wait for the environment to build (dependencies install automatically) |
| 12 | +- [License](#license) |
28 | 13 |
|
29 | | -#### 2. Local VS Code Dev Container |
| 14 | +## Prerequisites |
30 | 15 |
|
31 | | -**Requirements:** Docker + VS Code + Dev Containers extension |
| 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) |
32 | 23 |
|
33 | | -1. Open the repo in VS Code |
34 | | -2. When prompted, select **Reopen in Container** (or run `Dev Containers: Reopen in Container` from the Command Palette) |
35 | | -3. Wait for the container to build |
| 24 | +## Setup |
36 | 25 |
|
37 | | -#### 3. Local Machine Without a Dev Container |
38 | | - |
39 | | -If you prefer a plain local environment, use **uv** for dependency management: |
| 26 | +1. Install dependencies using `uv`: |
40 | 27 |
|
41 | 28 | ```bash |
42 | 29 | uv sync |
43 | 30 | ``` |
44 | 31 |
|
45 | | -## Using the Expenses MCP Server |
| 32 | +2. Copy `.env-sample` to `.env` and configure your environment variables: |
46 | 33 |
|
47 | | -### Run the MCP Server in VS Code with GitHub Copilot (Codespace/Local Dev Container/Local) |
| 34 | +```bash |
| 35 | +cp .env-sample .env |
| 36 | +``` |
48 | 37 |
|
49 | | -1. Open `.vscode/mcp.json` in the editor |
50 | | -1. Click the **Start** button (▶) above the server name `expenses-mcp` |
51 | | -1. Confirm in the output panel that the server is running |
52 | | -1. Open the GitHub Copilot Chat panel (bottom right, or via Command Palette: `GitHub Copilot: Focus Chat`) |
53 | | -1. Click the **Tools** icon (wrench) at the bottom of the chat panel |
54 | | -1. Ensure `expenses-mcp` is selected in the list of available tools |
55 | | -1. Ask Copilot to invoke the tool: |
56 | | - - "Use add_expense to record a $12 lunch today paid with visa" |
| 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`) |
57 | 43 |
|
58 | | -### MCP Inspector |
| 44 | +## Python Scripts |
59 | 45 |
|
60 | | -The MCP Inspector is a browser-based visual testing and debugging tool for MCP servers. At the moment it does not work great with non STDIO MCP Servers when working inside of a Codespace. |
| 46 | +Run any script with: `uv run <script_name>` |
61 | 47 |
|
62 | | -#### Launch the MCP Inspector and connect to STDIO MCP Server in GitHub Codespace |
| 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 integration |
| 51 | +- **agentframework_mcp_learn.py** - Microsoft Agent Framework integration with MCP |
63 | 52 |
|
64 | | -1. Run the following command in the terminal: |
65 | | - ```bash |
66 | | - .devcontainer/launch-inspector-codespace.sh |
67 | | - ``` |
68 | | -2. Note the **Inspector Proxy Address** and **Session Token** from the terminal output |
| 53 | +## MCP Server Configuration |
69 | 54 |
|
70 | | -3. In the **Ports** view, set port **6277** to **PUBLIC** visibility |
| 55 | +### Using with MCP Inspector |
71 | 56 |
|
72 | | -4. Access the Inspector UI and configure: |
73 | | - - **Transport Type**: `STDIO` |
74 | | - - **Command**: `uv` |
75 | | - - **Arguments**: `run main.py` |
76 | | - - Expand configuration area |
77 | | - - **Inspector Proxy Address**: (from terminal output) |
78 | | - - **Proxy Session Token**: (from terminal output) |
| 57 | +The [MCP Inspector](https://github.com/modelcontextprotocol/inspector) is a developer tool for testing and debugging MCP servers. |
79 | 58 |
|
80 | | -#### Launch the MCP Inspector and connect to STDIO MCP Server in VS Code and Dev Container |
| 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. |
81 | 60 |
|
82 | | -1. Run the following command in the terminal: |
83 | | - ```bash |
84 | | - HOST=0.0.0.0 DANGEROUSLY_OMIT_AUTH=true npx -y @modelcontextprotocol/inspector |
85 | | - ``` |
86 | | -1. Open `http://localhost:6274` in your browser |
87 | | -1. Access the Inspector UI and configure: |
88 | | - - **Transport Type**: `STDIO` |
89 | | - - **Command**: `uv` |
90 | | - - **Arguments**: `run main.py` |
| 61 | +**For stdio servers:** |
91 | 62 |
|
92 | | -> **Note:** `HOST=0.0.0.0` is required in devcontainer environments to bind the Inspector to all network interfaces, allowing proper communication between the UI and proxy server. `DANGEROUSLY_OMIT_AUTH=true` disables authentication - only use in trusted development environments. |
| 63 | +```bash |
| 64 | +npx @modelcontextprotocol/inspector uv run basic_mcp_stdio.py |
| 65 | +``` |
| 66 | + |
| 67 | +**For HTTP servers:** |
93 | 68 |
|
94 | | -#### Launch the inspector locally:** |
| 69 | +1. Start the HTTP server: |
| 70 | +```bash |
| 71 | +uv run basic_mcp_http.py |
| 72 | +``` |
95 | 73 |
|
96 | | -1. Run the following command in the terminal: |
97 | | - ```bash |
98 | | - npx @modelcontextprotocol/inspector uv run main.py |
99 | | - ``` |
100 | | -2. The Inspector will automatically open in your browser at `http://localhost:6274` |
| 74 | +2. In another terminal, run the inspector: |
| 75 | +```bash |
| 76 | +npx @modelcontextprotocol/inspector http://localhost:8000/mcp |
| 77 | +``` |
101 | 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 |
102 | 84 |
|
103 | | ---- |
| 85 | +### Using with GitHub Copilot |
104 | 86 |
|
105 | | -## Debugging |
| 87 | +The `.vscode/mcp.json` file configures MCP servers for GitHub Copilot integration: |
106 | 88 |
|
107 | | -You can attach the VS Code debugger to the running MCP server to set breakpoints and inspect code execution. |
| 89 | +**Available Servers:** |
108 | 90 |
|
109 | | -### Debugging STDIO MCP server with GitHub Copilot in Codespace/Dev Container/locally |
| 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 |
110 | 95 |
|
| 96 | +**Switching Servers:** |
111 | 97 |
|
112 | | -1. Open `.vscode/mcp.json` in the editor |
| 98 | +Configure which server GitHub Copilot uses by selecting it in the Chat panel selecting the tools icon. |
113 | 99 |
|
114 | | -2. Start the **expenses-mcp-debug** server (instead of expenses-mcp) |
| 100 | +## Debugging |
115 | 101 |
|
116 | | -3. In VS Code, open the Run and Debug panel (Ctrl+Shift+D / Cmd+Shift+D) |
| 102 | +### Debug Configurations |
117 | 103 |
|
118 | | -4. Select **"Attach to MCP Server"** from the dropdown and click the play button (or press F5) |
| 104 | +The `.vscode/launch.json` provides four debug configurations: |
119 | 105 |
|
120 | | -5. In GitHub Copilot Chat, make sure **expenses-mcp-debug** is selected in the tools menu |
| 106 | +#### Launch Configurations (Start server with debugging) |
121 | 107 |
|
122 | | -6. Set breakpoints in `main.py` and use the server from GitHub Copilot Chat. Breakpoints will be hit when tools are invoked. |
| 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 |
123 | 111 |
|
124 | | -### Debugging HTTP MCP server with GitHub Copilot in Codespace/Dev Container/locally |
| 112 | +2. **Launch MCP stdio Server (Debug)** |
| 113 | + - Directly starts `basic_mcp_stdio.py` with debugger attached |
| 114 | + - Best for: Testing stdio communication |
125 | 115 |
|
126 | | -1. Run the HTTP MCP Server in the terminal with `uv run -- python -m debugpy --listen 0.0.0.0:5678 main_http.py` |
| 116 | +#### Attach Configurations (Attach to running server) |
127 | 117 |
|
128 | | -1. Open `.vscode/mcp.json` in the editor |
| 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 |
129 | 121 |
|
130 | | -1. Start the **expenses-mcp-debug** server (instead of expenses-mcp) |
| 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 |
131 | 125 |
|
132 | | -1. In VS Code, open the Run and Debug panel (Ctrl+Shift+D / Cmd+Shift+D) |
| 126 | +### Debugging Workflow |
133 | 127 |
|
134 | | -1. Select **"Attach to MCP Server"** from the dropdown and click the play button (or press F5) |
| 128 | +#### Option 1: Launch and Debug (Standalone) |
135 | 129 |
|
136 | | -1. In GitHub Copilot Chat, make sure **expenses-mcp-debug** is selected in the tools menu |
| 130 | +Use this approach for debugging with MCP Inspector or LangChain scripts: |
137 | 131 |
|
138 | | -1. Set breakpoints in `main_http.py` and use the server from GitHub Copilot Chat. Breakpoints will be hit when tools are invoked. |
| 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 | 139 |
|
140 | | ---- |
| 140 | +#### Option 2: Attach to Running Server (Copilot Integration) |
141 | 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 |
142 | 151 |
|
143 | | -## Contributing |
| 152 | +## License |
144 | 153 |
|
145 | | -Contributions are welcome! Please feel free to submit issues, feature requests, or pull requests. |
| 154 | +MIT |
0 commit comments