|
1 | 1 | # Python MCP Demo |
2 | 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. |
| 3 | +A demonstration project showcasing Model Context Protocol (MCP) implementations using FastMCP, with examples of stdio and HTTP transports, integration with LangChain and Agent Framework, and deployment to Azure Container Apps. |
4 | 4 |
|
5 | 5 | ## Table of Contents |
6 | 6 |
|
7 | | -- [Prerequisites](#prerequisites) |
8 | | -- [Setup](#setup) |
9 | | -- [Python Scripts](#python-scripts) |
10 | | -- [MCP Server Configuration](#mcp-server-configuration) |
11 | | -- [Debugging](#debugging) |
| 7 | +- [Getting started](#getting-started) |
| 8 | + - [GitHub Codespaces](#github-codespaces) |
| 9 | + - [VS Code Dev Containers](#vs-code-dev-containers) |
| 10 | + - [Local environment](#local-environment) |
| 11 | +- [Run local MCP servers](#run-local-mcp-servers) |
| 12 | + - [Use with GitHub Copilot](#use-with-github-copilot) |
| 13 | + - [Debug with VS Code](#debug-with-vs-code) |
| 14 | + - [Inspect with MCP inspector](#inspect-with-mcp-inspector) |
| 15 | +- [Run local Agents <-> MCP](#run-local-agents---mcp) |
| 16 | +- [Deploy to Azure](#deploy-to-azure) |
| 17 | +- [Deploy to Azure with private networking](#deploy-to-azure-with-private-networking) |
12 | 18 |
|
13 | | -## Prerequisites |
| 19 | +## Getting started |
14 | 20 |
|
15 | | -- Python 3.13 or higher |
16 | | -- [uv](https://docs.astral.sh/uv/) |
17 | | -- API access to one of the following: |
18 | | - - GitHub Models (GitHub token) |
19 | | - - Azure OpenAI (Azure credentials) |
20 | | - - Ollama (local installation) |
21 | | - - OpenAI API (API key) |
| 21 | +You have a few options for setting up this project. The quickest way to get started is GitHub Codespaces, since it will setup all the tools for you, but you can also set it up locally. |
22 | 22 |
|
23 | | -## Setup |
| 23 | +### GitHub Codespaces |
24 | 24 |
|
25 | | -1. Install dependencies using `uv`: |
| 25 | +You can run this project virtually by using GitHub Codespaces. Click the button to open a web-based VS Code instance in your browser: |
| 26 | + |
| 27 | +[](https://codespaces.new/pamelafox/python-mcp-demo) |
| 28 | + |
| 29 | +Once the Codespace is open, open a terminal window and continue with the deployment steps. |
| 30 | + |
| 31 | +### VS Code Dev Containers |
| 32 | + |
| 33 | +A related option is VS Code Dev Containers, which will open the project in your local VS Code using the [Dev Containers extension](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers): |
| 34 | + |
| 35 | +1. Start Docker Desktop (install it if not already installed) |
| 36 | +2. Open the project: [](https://vscode.dev/redirect?url=vscode://ms-vscode-remote.remote-containers/cloneInVolume?url=https://github.com/pamelafox/python-mcp-demo) |
| 37 | +3. In the VS Code window that opens, once the project files show up (this may take several minutes), open a terminal window. |
| 38 | +4. Continue with the deployment steps. |
| 39 | + |
| 40 | +### Local environment |
| 41 | + |
| 42 | +If you're not using one of the above options, then you'll need to: |
| 43 | + |
| 44 | +1. Make sure the following tools are installed: |
| 45 | + - [Azure Developer CLI (azd)](https://aka.ms/install-azd) |
| 46 | + - [Python 3.13+](https://www.python.org/downloads/) |
| 47 | + - [Docker Desktop](https://www.docker.com/products/docker-desktop/) |
| 48 | + - [Git](https://git-scm.com/downloads) |
| 49 | + |
| 50 | +2. Clone the repository and open the project folder. |
| 51 | + |
| 52 | +3. Create a [Python virtual environment](https://docs.python.org/3/tutorial/venv.html#creating-virtual-environments) and activate it. |
| 53 | + |
| 54 | +4. Install the dependencies: |
26 | 55 |
|
27 | 56 | ```bash |
28 | 57 | uv sync |
29 | 58 | ``` |
30 | 59 |
|
31 | | -2. Copy `.env-sample` to `.env` and configure your environment variables: |
| 60 | +5. Copy `.env-sample` to `.env` and configure your environment variables: |
32 | 61 |
|
33 | 62 | ```bash |
34 | 63 | cp .env-sample .env |
35 | 64 | ``` |
36 | 65 |
|
37 | | -3. Edit `.env` with your API credentials. Choose one of the following providers by setting `API_HOST`: |
| 66 | +6. Edit `.env` with your API credentials. Choose one of the following providers by setting `API_HOST`: |
38 | 67 | - `github` - GitHub Models (requires `GITHUB_TOKEN`) |
39 | 68 | - `azure` - Azure OpenAI (requires Azure credentials) |
40 | 69 | - `ollama` - Local Ollama instance |
41 | 70 | - `openai` - OpenAI API (requires `OPENAI_API_KEY`) |
42 | 71 |
|
43 | | -## Python Scripts |
| 72 | +## Run local MCP servers |
| 73 | + |
| 74 | +This project includes two MCP servers in the [`servers/`](servers/) directory: |
44 | 75 |
|
45 | | -Run any script with: `uv run <script_path>` |
| 76 | +| File | Description | |
| 77 | +|------|-------------| |
| 78 | +| [servers/basic_mcp_stdio.py](servers/basic_mcp_stdio.py) | MCP server with stdio transport for VS Code integration | |
| 79 | +| [servers/basic_mcp_http.py](servers/basic_mcp_http.py) | MCP server with HTTP transport on port 8000 | |
46 | 80 |
|
47 | | -- **servers/basic_mcp_http.py** - MCP server with HTTP transport on port 8000 |
48 | | -- **servers/basic_mcp_stdio.py** - MCP server with stdio transport for VS Code integration |
49 | | -- **agents/langchainv1_http.py** - LangChain agent with MCP integration |
50 | | -- **agents/langchainv1_github.py** - LangChain tool filtering demo with GitHub MCP (requires `GITHUB_TOKEN`) |
51 | | -- **agents/agentframework_learn.py** - Microsoft Agent Framework integration with MCP |
52 | | -- **agents/agentframework_http.py** - Microsoft Agent Framework integration with local Expenses MCP server |
| 81 | +Both servers implement an "Expenses Tracker" with a tool to add expenses to a CSV file. |
53 | 82 |
|
54 | | -## MCP Server Configuration |
| 83 | +### Use with GitHub Copilot |
55 | 84 |
|
56 | | -### Using with MCP Inspector |
| 85 | +The `.vscode/mcp.json` file configures MCP servers for GitHub Copilot integration: |
| 86 | + |
| 87 | +**Available Servers:** |
| 88 | + |
| 89 | +- **expenses-mcp**: stdio transport server for production use |
| 90 | +- **expenses-mcp-debug**: stdio server with debugpy on port 5678 |
| 91 | +- **expenses-mcp-http**: HTTP transport server at `http://localhost:8000/mcp`. You must start this server manually with `uv run servers/basic_mcp_http.py` before using it. |
| 92 | + |
| 93 | +**Switching Servers:** |
| 94 | + |
| 95 | +Configure which server GitHub Copilot uses by opening the Chat panel, selecting the tools icon, and choosing the desired MCP server from the list. |
| 96 | + |
| 97 | + |
| 98 | + |
| 99 | +**Example input:** |
| 100 | + |
| 101 | +Use a query like this to test the expenses MCP server: |
| 102 | + |
| 103 | +```text |
| 104 | +Log expense for 50 bucks of pizza on my amex today |
| 105 | +``` |
| 106 | + |
| 107 | + |
| 108 | + |
| 109 | +### Debug with VS Code |
| 110 | + |
| 111 | +The `.vscode/launch.json` provides a debug configuration to attach to an MCP server. |
| 112 | + |
| 113 | +**To debug an MCP server with GitHub Copilot Chat:** |
| 114 | + |
| 115 | +1. Set breakpoints in the MCP server code in `servers/basic_mcp_stdio.py` |
| 116 | +2. Start the debug server via `mcp.json` configuration by selecting `expenses-mcp-debug` |
| 117 | +3. Press `Cmd+Shift+D` to open Run and Debug |
| 118 | +4. Select "Attach to MCP Server (stdio)" configuration |
| 119 | +5. Press `F5` or the play button to start the debugger |
| 120 | +6. Select the expenses-mcp-debug server in GitHub Copilot Chat tools |
| 121 | +7. Use GitHub Copilot Chat to trigger the MCP tools |
| 122 | +8. Debugger pauses at breakpoints |
| 123 | + |
| 124 | +### Inspect with MCP inspector |
57 | 125 |
|
58 | 126 | The [MCP Inspector](https://github.com/modelcontextprotocol/inspector) is a developer tool for testing and debugging MCP servers. |
59 | 127 |
|
@@ -86,45 +154,125 @@ The inspector provides a web interface to: |
86 | 154 | - Inspect server responses and errors |
87 | 155 | - Debug server communication |
88 | 156 |
|
89 | | -### Using with GitHub Copilot |
| 157 | +--- |
90 | 158 |
|
91 | | -The `.vscode/mcp.json` file configures MCP servers for GitHub Copilot integration: |
| 159 | +## Run local Agents <-> MCP |
92 | 160 |
|
93 | | -**Available Servers:** |
| 161 | +This project includes example agents in the [`agents/`](agents/) directory that demonstrate how to connect AI agents to MCP servers: |
94 | 162 |
|
95 | | -- **expenses-mcp**: stdio transport server for production use |
96 | | -- **expenses-mcp-debug**: stdio server with debugpy on port 5678 |
97 | | -- **expenses-mcp-http**: HTTP transport server at `http://localhost:8000/mcp`. You must start this server manually with `uv run servers/basic_mcp_http.py` before using it. |
| 163 | +| File | Description | |
| 164 | +|------|-------------| |
| 165 | +| [agents/agentframework_learn.py](agents/agentframework_learn.py) | Microsoft Agent Framework integration with MCP | |
| 166 | +| [agents/agentframework_http.py](agents/agentframework_http.py) | Microsoft Agent Framework integration with local Expenses MCP server | |
| 167 | +| [agents/langchainv1_http.py](agents/langchainv1_http.py) | LangChain agent with MCP integration | |
| 168 | +| [agents/langchainv1_github.py](agents/langchainv1_github.py) | LangChain tool filtering demo with GitHub MCP (requires `GITHUB_TOKEN`) | |
98 | 169 |
|
99 | | -**Switching Servers:** |
| 170 | +**To run an agent:** |
100 | 171 |
|
101 | | -Configure which server GitHub Copilot uses by opening the Chat panel, selecting the tools icon, and choosing the desired MCP server from the list. |
| 172 | +1. First start the HTTP MCP server: |
102 | 173 |
|
103 | | - |
| 174 | + ```bash |
| 175 | + uv run servers/basic_mcp_http.py |
| 176 | + ``` |
104 | 177 |
|
105 | | -**Example input** |
| 178 | +2. In another terminal, run an agent: |
106 | 179 |
|
107 | | -Use a query like this to test the expenses MCP server: |
| 180 | + ```bash |
| 181 | + uv run agents/agentframework_http.py |
| 182 | + ``` |
108 | 183 |
|
109 | | -``` |
110 | | -Log expense for 50 bucks of pizza on my amex today |
111 | | -``` |
| 184 | +The agents will connect to the MCP server and allow you to interact with the expense tracking tools through a chat interface. |
112 | 185 |
|
113 | | - |
| 186 | +--- |
114 | 187 |
|
115 | | -## Debugging |
| 188 | +## Deploy to Azure |
116 | 189 |
|
117 | | -The `.vscode/launch.json` provides one debug configuration: |
| 190 | +This project can be deployed to Azure Container Apps using the Azure Developer CLI (azd). The deployment provisions: |
118 | 191 |
|
119 | | -**Attach to MCP Server (stdio)**: Attaches to server started via `expenses-mcp-debug` in `mcp.json` |
| 192 | +- **Azure Container Apps** - Hosts both the MCP server and agent |
| 193 | +- **Azure OpenAI** - Provides the LLM for the agent |
| 194 | +- **Azure Cosmos DB** - Stores expenses data |
| 195 | +- **Azure Container Registry** - Stores container images |
| 196 | +- **Log Analytics** - Monitoring and diagnostics |
120 | 197 |
|
121 | | -To debug an MCP server with GitHub Copilot Chat: |
| 198 | +### Azure account setup |
122 | 199 |
|
123 | | -1. Set breakpoints in the MCP server code in `servers/basic_mcp_stdio.py` |
124 | | -1. Start the debug server via `mcp.json` configuration by selecting `expenses-mcp-debug` |
125 | | -1. Press `Cmd+Shift+D` to open Run and Debug |
126 | | -1. Select "Attach to MCP Server (stdio)" configuration |
127 | | -1. Press `F5` or the play button to start the debugger |
128 | | -1. Select the expenses-mcp-debug server in GitHub Copilot Chat tools |
129 | | -1. Use GitHub Copilot Chat to trigger the MCP tools |
130 | | -1. Debugger pauses at breakpoints |
| 200 | +1. Sign up for a [free Azure account](https://azure.microsoft.com/free/) and create an Azure Subscription. |
| 201 | +2. Check that you have the necessary permissions: |
| 202 | + - Your Azure account must have `Microsoft.Authorization/roleAssignments/write` permissions, such as [Role Based Access Control Administrator](https://learn.microsoft.com/azure/role-based-access-control/built-in-roles#role-based-access-control-administrator-preview), [User Access Administrator](https://learn.microsoft.com/azure/role-based-access-control/built-in-roles#user-access-administrator), or [Owner](https://learn.microsoft.com/azure/role-based-access-control/built-in-roles#owner). |
| 203 | + - Your Azure account also needs `Microsoft.Resources/deployments/write` permissions on the subscription level. |
| 204 | + |
| 205 | +### Deploying with azd |
| 206 | + |
| 207 | +1. Login to Azure: |
| 208 | + |
| 209 | + ```bash |
| 210 | + azd auth login |
| 211 | + ``` |
| 212 | + |
| 213 | + For GitHub Codespaces users, if the previous command fails, try: |
| 214 | + |
| 215 | + ```bash |
| 216 | + azd auth login --use-device-code |
| 217 | + ``` |
| 218 | + |
| 219 | +2. Create a new azd environment: |
| 220 | + |
| 221 | + ```bash |
| 222 | + azd env new |
| 223 | + ``` |
| 224 | + |
| 225 | + This will create a folder inside `.azure` with the name of your environment. |
| 226 | + |
| 227 | +3. Provision and deploy the resources: |
| 228 | + |
| 229 | + ```bash |
| 230 | + azd up |
| 231 | + ``` |
| 232 | + |
| 233 | + It will prompt you to select a subscription and location. This will take several minutes to complete. |
| 234 | + |
| 235 | +4. Once deployment is complete, a `.env` file will be created with the necessary environment variables to run the agents locally against the deployed resources. |
| 236 | + |
| 237 | +### Costs |
| 238 | + |
| 239 | +Pricing varies per region and usage, so it isn't possible to predict exact costs for your usage. |
| 240 | + |
| 241 | +You can try the [Azure pricing calculator](https://azure.com/e/3987c81282c84410b491d28094030c9a) for the resources: |
| 242 | + |
| 243 | +- **Azure OpenAI Service**: S0 tier, GPT-4o-mini model. Pricing is based on token count. [Pricing](https://azure.microsoft.com/pricing/details/cognitive-services/openai-service/) |
| 244 | +- **Azure Container Apps**: Consumption tier. [Pricing](https://azure.microsoft.com/pricing/details/container-apps/) |
| 245 | +- **Azure Container Registry**: Standard tier. [Pricing](https://azure.microsoft.com/pricing/details/container-registry/) |
| 246 | +- **Azure Cosmos DB**: Serverless tier. [Pricing](https://azure.microsoft.com/pricing/details/cosmos-db/) |
| 247 | +- **Log Analytics** (Optional): Pay-as-you-go tier. Costs based on data ingested. [Pricing](https://azure.microsoft.com/pricing/details/monitor/) |
| 248 | + |
| 249 | +⚠️ To avoid unnecessary costs, remember to take down your app if it's no longer in use, either by deleting the resource group in the Portal or running `azd down`. |
| 250 | + |
| 251 | +--- |
| 252 | + |
| 253 | +## Deploy to Azure with private networking |
| 254 | + |
| 255 | +To demonstrate enhanced security for production deployments, this project supports deploying with a virtual network (VNet) configuration that restricts public access to Azure resources. |
| 256 | + |
| 257 | +1. Set these azd environment variables to set up a virtual network and private endpoints for the Container App, Cosmos DB, and OpenAI resources: |
| 258 | + |
| 259 | + ```bash |
| 260 | + azd env set USE_VNET true |
| 261 | + azd env set USE_PRIVATE_INGRESS true |
| 262 | + ``` |
| 263 | + |
| 264 | + The Log Analytics and ACR resources will still have public access enabled, so that you can deploy and monitor the app without needing a VPN. In production, you would typically restrict these as well. |
| 265 | + |
| 266 | +2. Provision and deploy: |
| 267 | + |
| 268 | + ```bash |
| 269 | + azd up |
| 270 | + ``` |
| 271 | + |
| 272 | +### Additional costs for private networking |
| 273 | + |
| 274 | +When using VNet configuration, additional Azure resources are provisioned: |
| 275 | + |
| 276 | +- **Virtual Network**: Pay-as-you-go tier. Costs based on data processed. [Pricing](https://azure.microsoft.com/pricing/details/virtual-network/) |
| 277 | +- **Azure Private DNS Resolver**: Pricing per month, endpoints, and zones. [Pricing](https://azure.microsoft.com/pricing/details/dns/) |
| 278 | +- **Azure Private Endpoints**: Pricing per hour per endpoint. [Pricing](https://azure.microsoft.com/pricing/details/private-link/) |
0 commit comments