Skip to content

Commit

Permalink
First version of mcp-tunnel (microsoft#350)
Browse files Browse the repository at this point in the history
Supports arbitrary SSE MCP servers, with defaults that match our vscode
and office ports.
- Sets up tunnel with `devtunnel`
- Gets URI and access token for tunnel
- Generates assistant config file to be imported
  • Loading branch information
markwaddle authored Feb 26, 2025
1 parent 83ec66f commit 8a68d6a
Show file tree
Hide file tree
Showing 12 changed files with 772 additions and 55 deletions.
48 changes: 1 addition & 47 deletions assistants/codespace-assistant/uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions libraries/python/assistant-extensions/uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions libraries/python/mcp-extensions/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ readme = "README.md"
requires-python = ">=3.11"
dependencies = [
"deepmerge>=2.0",
"mcp>=1.2.1",
"pytest-asyncio>=0.25.3",
"mcp>=1.2.1",
]

[project.optional-dependencies]
Expand All @@ -23,5 +22,4 @@ requires = ["hatchling"]
build-backend = "hatchling.build"

[dependency-groups]
dev = ["pyright>=1.1.389", "pytest>=8.3.1"]

dev = ["pyright>=1.1.389", "pytest>=8.3.1", "pytest-asyncio>=0.25.3"]
4 changes: 2 additions & 2 deletions libraries/python/mcp-extensions/uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 57 additions & 0 deletions libraries/python/mcp-tunnel/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{
"editor.bracketPairColorization.enabled": true,
"editor.codeActionsOnSave": {
"source.organizeImports": "explicit",
"source.fixAll": "explicit"
},
"editor.guides.bracketPairs": "active",
"editor.formatOnPaste": true,
"editor.formatOnType": true,
"editor.formatOnSave": true,
"files.eol": "\n",
"files.trimTrailingWhitespace": true,
"python.analysis.autoFormatStrings": true,
"python.analysis.autoImportCompletions": true,
"python.analysis.diagnosticMode": "workspace",
"python.analysis.fixAll": [
"source.unusedImports"
],
"python.analysis.inlayHints.functionReturnTypes": true,
"python.analysis.typeCheckingMode": "standard",
"python.defaultInterpreterPath": "${workspaceFolder}/.venv",
"python.testing.pytestEnabled": false,
"[python]": {
"editor.defaultFormatter": "charliermarsh.ruff",
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.fixAll": "explicit",
"source.unusedImports": "explicit",
"source.organizeImports": "explicit",
"source.formatDocument": "explicit"
}
},
"ruff.nativeServer": "on",
"search.exclude": {
"**/.venv": true,
"**/.data": true,
"**/__pycache__": true
},
// For use with optional extension: "streetsidesoftware.code-spell-checker"
"cSpell.ignorePaths": [
".venv",
"node_modules",
"package-lock.json",
"settings.json",
"uv.lock"
],
"cSpell.words": [
"asyncio",
"deepmerge",
"fastmcp",
"interoperating",
"Lifecycles",
"pydantic",
"pyright",
"pytest"
]
}
2 changes: 2 additions & 0 deletions libraries/python/mcp-tunnel/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
repo_root = $(shell git rev-parse --show-toplevel)
include $(repo_root)/tools/makefiles/python.mk
103 changes: 103 additions & 0 deletions libraries/python/mcp-tunnel/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
# MCP Tunnel

A command-line tool for managing secure tunnels to local Model Context Protocol (MCP) servers.

## Overview

MCP Tunnel simplifies the process of exposing locally running MCP servers to the internet through secure tunnels. This enables AI assistants like Codespace assistant to connect to your local MCP servers, allowing them to access local resources, files, and functionality.

The tool uses Microsoft's DevTunnel service to create secure tunnels from the internet to your local machine. It can manage multiple tunnels simultaneously and generates the necessary configuration files for connecting your Codespace assistant to these tunnels.

## Prerequisites

- Python 3.11 or higher
- [Microsoft DevTunnel CLI](https://learn.microsoft.com/en-us/azure/developer/dev-tunnels/get-started) installed and available in your PATH
- A Microsoft account (to log in to DevTunnel)

## Installation

Install MCP Tunnel using make from the `mcp-tunnel` directory:

```bash
make install
```

## Usage

### Basic Usage

Start tunnels for default MCP servers (vscode on port 6010 and office on port 25566):

```bash
mcp-tunnel
```

### Custom Servers

Specify custom server names and ports:

```bash
mcp-tunnel --servers "myserver:8080,anotherserver:9000"
```

### Output

When you run MCP Tunnel, it will:

1. Check if DevTunnel CLI is installed and you're logged in
2. Create tunnels for each specified server
3. Start tunnel processes and display their output with color-coding
4. Generate a configuration file at `~/.mcp-tunnel/config.yaml`
5. Keep tunnels running until you press Ctrl+C

Example output:

```
DevTunnel CLI detected and user is logged in
Starting tunnels for servers: vscode, office
Starting tunnel for vscode on port 6010...
Starting tunnel for office on port 25566...
[vscode] Tunnel vscode-a1b2c3 is online and ready to use
[vscode] Using tunnel URL: https://a1b2c3d4-6010.usw2.devtunnels.ms
[office] Tunnel office-a1b2c3 is online and ready to use
[office] Using tunnel URL: https://e5f6g7h8-25566.usw2.devtunnels.ms
Config file written to: /home/user/.mcp-tunnel/config.yaml
All tunnels started. Press Ctrl+C to stop all tunnels.
```

## Configuration

MCP Tunnel generates a configuration file at `~/.mcp-tunnel/config.yaml` that can be used to connect your AI assistant to the tunnels. The configuration includes:

- SSE endpoints for each tunnel
- Authentication headers

You can use this configuration with the Codespace assistant by importing it from the Assistant Configuration screen.

## Troubleshooting

### DevTunnel CLI Not Found

If you see an error about the DevTunnel CLI not being found:

1. Install the DevTunnel CLI by following the [official instructions](https://learn.microsoft.com/en-us/azure/developer/dev-tunnels/get-started)
2. Make sure it's in your PATH
3. Test that it works by running `devtunnel --version`

### Not Logged In

If you're not logged in to DevTunnel:

```bash
devtunnel login
```

### Tunnels Not Starting

If tunnels aren't starting, check:

1. That the ports you specified are correct
2. That your local MCP servers are running on those ports
3. That the ports aren't being blocked by a firewall
3 changes: 3 additions & 0 deletions libraries/python/mcp-tunnel/mcp_tunnel/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from ._main import tunnel_servers

__all__ = ["tunnel_servers"]
Loading

0 comments on commit 8a68d6a

Please sign in to comment.