mcp-embedded-ui 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- mcp_embedded_ui-0.1.0/.claude/settings.local.json +17 -0
- mcp_embedded_ui-0.1.0/.github/CODEOWNERS +3 -0
- mcp_embedded_ui-0.1.0/.github/copilot-ignore +13 -0
- mcp_embedded_ui-0.1.0/.github/workflows/ci.yml +50 -0
- mcp_embedded_ui-0.1.0/.gitignore +9 -0
- mcp_embedded_ui-0.1.0/CHANGELOG.md +26 -0
- mcp_embedded_ui-0.1.0/PKG-INFO +219 -0
- mcp_embedded_ui-0.1.0/README.md +185 -0
- mcp_embedded_ui-0.1.0/examples/fastapi_demo.py +56 -0
- mcp_embedded_ui-0.1.0/pyproject.toml +77 -0
- mcp_embedded_ui-0.1.0/src/mcp_embedded_ui/__init__.py +29 -0
- mcp_embedded_ui-0.1.0/src/mcp_embedded_ui/html.py +401 -0
- mcp_embedded_ui-0.1.0/src/mcp_embedded_ui/py.typed +0 -0
- mcp_embedded_ui-0.1.0/src/mcp_embedded_ui/server.py +271 -0
- mcp_embedded_ui-0.1.0/tests/__init__.py +0 -0
- mcp_embedded_ui-0.1.0/tests/test_server.py +618 -0
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"permissions": {
|
|
3
|
+
"allow": [
|
|
4
|
+
"Bash(pip install:*)",
|
|
5
|
+
"Bash(python:*)",
|
|
6
|
+
"Bash(python3:*)",
|
|
7
|
+
"Skill(spec-forge:decompose)",
|
|
8
|
+
"Bash(mv:*)",
|
|
9
|
+
"Skill(apcore-skills:sync)",
|
|
10
|
+
"WebSearch",
|
|
11
|
+
"Bash(gh api:*)",
|
|
12
|
+
"WebFetch(domain:raw.githubusercontent.com)",
|
|
13
|
+
"Bash(gh:*)",
|
|
14
|
+
"Bash(do echo:*)"
|
|
15
|
+
]
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
name: CI
|
|
2
|
+
|
|
3
|
+
permissions:
|
|
4
|
+
contents: read
|
|
5
|
+
|
|
6
|
+
on:
|
|
7
|
+
push:
|
|
8
|
+
branches: [ "main" ]
|
|
9
|
+
pull_request:
|
|
10
|
+
branches: [ "main" ]
|
|
11
|
+
|
|
12
|
+
jobs:
|
|
13
|
+
test:
|
|
14
|
+
name: Test on Python ${{ matrix.python-version }}
|
|
15
|
+
runs-on: ubuntu-latest
|
|
16
|
+
# container:
|
|
17
|
+
# image: python:${{ matrix.python-version }}
|
|
18
|
+
strategy:
|
|
19
|
+
fail-fast: false
|
|
20
|
+
matrix:
|
|
21
|
+
# Pin CI to a single, consistent Python version to reduce version-related
|
|
22
|
+
# test flakiness. Use 3.12 for CI stability; expand matrix later if desired.
|
|
23
|
+
python-version: ["3.12"]
|
|
24
|
+
|
|
25
|
+
steps:
|
|
26
|
+
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
|
|
27
|
+
|
|
28
|
+
- name: Set up Python
|
|
29
|
+
uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5.6.0
|
|
30
|
+
with:
|
|
31
|
+
python-version: ${{ matrix.python-version }}
|
|
32
|
+
cache: pip
|
|
33
|
+
|
|
34
|
+
- name: Install dependencies
|
|
35
|
+
run: |
|
|
36
|
+
python -m pip install --upgrade pip
|
|
37
|
+
# If a constraints.txt file is present, use it to pin critical dependency versions.
|
|
38
|
+
if [ -f constraints.txt ]; then
|
|
39
|
+
pip install -e ".[dev]" -c constraints.txt
|
|
40
|
+
else
|
|
41
|
+
pip install -e ".[dev]"
|
|
42
|
+
fi
|
|
43
|
+
|
|
44
|
+
- name: Lint with Ruff
|
|
45
|
+
run: |
|
|
46
|
+
ruff check src/ tests/
|
|
47
|
+
|
|
48
|
+
- name: Run tests
|
|
49
|
+
run: |
|
|
50
|
+
pytest
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
|
|
5
|
+
Format follows [Keep a Changelog](https://keepachangelog.com/).
|
|
6
|
+
|
|
7
|
+
## [0.1.0] - 2026-03-09
|
|
8
|
+
|
|
9
|
+
### Added
|
|
10
|
+
|
|
11
|
+
- Core ASGI route builder (`build_ui_routes`) with 5 endpoints:
|
|
12
|
+
- `GET /` — self-contained HTML explorer page
|
|
13
|
+
- `GET /meta` — JSON config (`allow_execute`, `title`)
|
|
14
|
+
- `GET /tools` — tool summary list
|
|
15
|
+
- `GET /tools/{name}` — tool detail with input schema
|
|
16
|
+
- `POST /tools/{name}/call` — tool execution
|
|
17
|
+
- High-level factories: `create_app()` (standalone ASGI) and `create_mount()` (Starlette/FastAPI prefix mount)
|
|
18
|
+
- Dynamic tools support — static list, sync callable, or async callable
|
|
19
|
+
- Auth hook — sync and async context manager patterns
|
|
20
|
+
- Configurable title with XSS-safe HTML escaping
|
|
21
|
+
- `annotations` field omitted (not `null`) when absent
|
|
22
|
+
- `_meta` with `_trace_id` omitted when trace ID is empty
|
|
23
|
+
- `allow_execute=False` blocks at handler level, not just UI
|
|
24
|
+
- Auth error responses return only `{"error": "Unauthorized"}` — no detail leaking
|
|
25
|
+
- Backward-compatible `build_mcp_ui_routes()` alias with `DeprecationWarning`
|
|
26
|
+
- 42 tests covering all endpoints, auth, dynamic tools, security, and exports
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: mcp-embedded-ui
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A lightweight, minimal-dependency embedded Web UI for any MCP Server.
|
|
5
|
+
Project-URL: Homepage, https://aipartnerup.com
|
|
6
|
+
Project-URL: Documentation, https://github.com/aipartnerup/mcp-embedded-ui
|
|
7
|
+
Project-URL: Repository, https://github.com/aipartnerup/mcp-embedded-ui-python
|
|
8
|
+
Project-URL: Issues, https://github.com/aipartnerup/mcp-embedded-ui-python/issues
|
|
9
|
+
Project-URL: Changelog, https://github.com/aipartnerup/mcp-embedded-ui-python/blob/main/CHANGELOG.md
|
|
10
|
+
Author-email: aipartnerup <team@aipartnerup.com>
|
|
11
|
+
License-Expression: Apache-2.0
|
|
12
|
+
Keywords: asgi,explorer,fastapi,mcp,model-context-protocol,starlette,tools,ui
|
|
13
|
+
Classifier: Development Status :: 3 - Alpha
|
|
14
|
+
Classifier: Intended Audience :: Developers
|
|
15
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
21
|
+
Classifier: Topic :: Internet :: WWW/HTTP
|
|
22
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
23
|
+
Requires-Python: >=3.10
|
|
24
|
+
Requires-Dist: starlette>=0.14.0
|
|
25
|
+
Provides-Extra: dev
|
|
26
|
+
Requires-Dist: fastapi; extra == 'dev'
|
|
27
|
+
Requires-Dist: httpx; extra == 'dev'
|
|
28
|
+
Requires-Dist: mypy>=1.0; extra == 'dev'
|
|
29
|
+
Requires-Dist: pytest-cov>=4.0; extra == 'dev'
|
|
30
|
+
Requires-Dist: pytest>=7.0; extra == 'dev'
|
|
31
|
+
Requires-Dist: ruff>=0.1; extra == 'dev'
|
|
32
|
+
Requires-Dist: uvicorn; extra == 'dev'
|
|
33
|
+
Description-Content-Type: text/markdown
|
|
34
|
+
|
|
35
|
+
# mcp-embedded-ui (Python)
|
|
36
|
+
|
|
37
|
+
The Python implementation of [mcp-embedded-ui](https://github.com/aipartnerup/mcp-embedded-ui) — a browser-based tool explorer for any [MCP](https://modelcontextprotocol.io/) (Model Context Protocol) server.
|
|
38
|
+
|
|
39
|
+
## What is this?
|
|
40
|
+
|
|
41
|
+
If you build an MCP server in Python, your users interact with tools through raw JSON — no visual feedback, no schema browser, no quick way to test. This library adds a full browser UI to your server with **one import and one mount**.
|
|
42
|
+
|
|
43
|
+
```
|
|
44
|
+
┌───────────────────────────────────┐
|
|
45
|
+
│ Browser │
|
|
46
|
+
│ Tool list → Schema → Try it │
|
|
47
|
+
└──────────────┬────────────────────┘
|
|
48
|
+
│ HTTP / JSON
|
|
49
|
+
┌──────────────▼────────────────────┐
|
|
50
|
+
│ Your Python MCP Server │
|
|
51
|
+
│ + mcp-embedded-ui │
|
|
52
|
+
│ (FastAPI / Starlette / ASGI) │
|
|
53
|
+
└───────────────────────────────────┘
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## What does the UI provide?
|
|
57
|
+
|
|
58
|
+
- **Tool list** — browse all registered tools with descriptions and annotation badges
|
|
59
|
+
- **Schema inspector** — expand any tool to view its full JSON Schema (`inputSchema`)
|
|
60
|
+
- **Try-it console** — type JSON arguments, execute the tool, see results instantly
|
|
61
|
+
- **cURL export** — copy a ready-made cURL command for any execution
|
|
62
|
+
- **Auth support** — enter a Bearer token in the UI, sent with all requests
|
|
63
|
+
|
|
64
|
+
No build step. No CDN. No external dependencies. The entire UI is a single self-contained HTML page embedded in the package.
|
|
65
|
+
|
|
66
|
+
## Install
|
|
67
|
+
|
|
68
|
+
```bash
|
|
69
|
+
pip install mcp-embedded-ui
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
Requires Python 3.10+ and [Starlette](https://www.starlette.io/) >= 0.35.
|
|
73
|
+
|
|
74
|
+
## Quick Start
|
|
75
|
+
|
|
76
|
+
### FastAPI / Starlette
|
|
77
|
+
|
|
78
|
+
```python
|
|
79
|
+
from fastapi import FastAPI
|
|
80
|
+
from mcp_embedded_ui import create_mount
|
|
81
|
+
|
|
82
|
+
app = FastAPI()
|
|
83
|
+
|
|
84
|
+
# Mount at /explorer (default)
|
|
85
|
+
app.routes.append(create_mount(tools=my_tools, handle_call=my_handler))
|
|
86
|
+
|
|
87
|
+
# Or specify a custom prefix
|
|
88
|
+
app.routes.append(create_mount("/mcp-ui", tools=my_tools, handle_call=my_handler))
|
|
89
|
+
|
|
90
|
+
# Visit http://localhost:8000/explorer/
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
### Any ASGI framework
|
|
94
|
+
|
|
95
|
+
```python
|
|
96
|
+
from mcp_embedded_ui import create_app
|
|
97
|
+
|
|
98
|
+
# Returns a standard ASGI app — mount in any ASGI-compatible framework
|
|
99
|
+
ui_app = create_app(tools=my_tools, handle_call=my_handler)
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
### Full working example
|
|
103
|
+
|
|
104
|
+
```python
|
|
105
|
+
from fastapi import FastAPI
|
|
106
|
+
from mcp_embedded_ui import create_mount
|
|
107
|
+
|
|
108
|
+
# 1. Define your tools (any object with .name, .description, .inputSchema)
|
|
109
|
+
class MyTool:
|
|
110
|
+
def __init__(self, name, description, input_schema):
|
|
111
|
+
self.name = name
|
|
112
|
+
self.description = description
|
|
113
|
+
self.inputSchema = input_schema
|
|
114
|
+
|
|
115
|
+
tools = [
|
|
116
|
+
MyTool("greet", "Say hello", {
|
|
117
|
+
"type": "object",
|
|
118
|
+
"properties": {"name": {"type": "string"}},
|
|
119
|
+
}),
|
|
120
|
+
]
|
|
121
|
+
|
|
122
|
+
# 2. Define a handler: (name, args) -> (content, is_error, trace_id)
|
|
123
|
+
async def handle_call(name, args):
|
|
124
|
+
if name == "greet":
|
|
125
|
+
return [{"type": "text", "text": f"Hello, {args.get('name', 'world')}!"}], False, None
|
|
126
|
+
return [{"type": "text", "text": f"Unknown tool: {name}"}], True, None
|
|
127
|
+
|
|
128
|
+
# 3. Mount the UI
|
|
129
|
+
app = FastAPI()
|
|
130
|
+
app.routes.append(create_mount(tools=tools, handle_call=handle_call))
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
### Dynamic tools
|
|
134
|
+
|
|
135
|
+
```python
|
|
136
|
+
# Sync callable — re-evaluated on every request
|
|
137
|
+
def get_tools():
|
|
138
|
+
return registry.list_tools()
|
|
139
|
+
|
|
140
|
+
# Async callable
|
|
141
|
+
async def get_tools():
|
|
142
|
+
return await registry.async_list_tools()
|
|
143
|
+
|
|
144
|
+
app = create_app(tools=get_tools, handle_call=my_handler)
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
## API
|
|
148
|
+
|
|
149
|
+
### Three-tier API
|
|
150
|
+
|
|
151
|
+
| Function | Returns | Use case |
|
|
152
|
+
|----------|---------|----------|
|
|
153
|
+
| `create_mount(prefix, *, tools, handle_call, **config)` | `Mount` | FastAPI / Starlette — mount under a URL prefix |
|
|
154
|
+
| `create_app(tools, handle_call, **config)` | `ASGIApp` | Any ASGI framework — standalone app |
|
|
155
|
+
| `build_ui_routes(tools, handle_call, **config)` | `list[Route]` | Power users — fine-grained route control |
|
|
156
|
+
|
|
157
|
+
### Parameters
|
|
158
|
+
|
|
159
|
+
| Parameter | Type | Default | Description |
|
|
160
|
+
|-----------|------|---------|-------------|
|
|
161
|
+
| `tools` | `list \| Callable \| AsyncCallable` | _required_ | MCP Tool objects (`.name`, `.description`, `.inputSchema`) |
|
|
162
|
+
| `handle_call` | `ToolCallHandler` | _required_ | `async (name, args) -> (content, is_error, trace_id)` |
|
|
163
|
+
| `allow_execute` | `bool` | `True` | Enable/disable tool execution (enforced server-side) |
|
|
164
|
+
| `auth_hook` | `AuthHook \| None` | `None` | Sync/async context manager factory for auth |
|
|
165
|
+
| `title` | `str` | `"MCP Tool Explorer"` | Page title (HTML-escaped automatically) |
|
|
166
|
+
|
|
167
|
+
### Auth Hook
|
|
168
|
+
|
|
169
|
+
The `auth_hook` receives a Starlette `Request` and returns a context manager (sync or async). Raise inside to reject with 401. The error response is always `{"error": "Unauthorized"}` — internal details are never leaked.
|
|
170
|
+
|
|
171
|
+
```python
|
|
172
|
+
from contextlib import contextmanager
|
|
173
|
+
|
|
174
|
+
@contextmanager
|
|
175
|
+
def my_auth(request):
|
|
176
|
+
token = request.headers.get("Authorization")
|
|
177
|
+
if not valid(token):
|
|
178
|
+
raise ValueError("Bad token")
|
|
179
|
+
my_identity_var.set(decode(token))
|
|
180
|
+
yield
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
Auth only guards `POST /tools/{name}/call`. Discovery endpoints (`GET /tools`, `GET /tools/{name}`, `GET /meta`) are always public.
|
|
184
|
+
|
|
185
|
+
### Endpoints
|
|
186
|
+
|
|
187
|
+
| Method | Path | Description |
|
|
188
|
+
|--------|------|-------------|
|
|
189
|
+
| GET | `/` | Self-contained HTML explorer page |
|
|
190
|
+
| GET | `/meta` | JSON config — `{ title, allow_execute }` |
|
|
191
|
+
| GET | `/tools` | Summary list of all tools |
|
|
192
|
+
| GET | `/tools/{name}` | Full tool detail with `inputSchema` |
|
|
193
|
+
| POST | `/tools/{name}/call` | Execute a tool, returns MCP `CallToolResult` |
|
|
194
|
+
|
|
195
|
+
## Development
|
|
196
|
+
|
|
197
|
+
```bash
|
|
198
|
+
# Install in editable mode with dev dependencies
|
|
199
|
+
pip install -e ".[dev]"
|
|
200
|
+
|
|
201
|
+
# Run the demo
|
|
202
|
+
python examples/fastapi_demo.py
|
|
203
|
+
# Visit http://localhost:8000/explorer/
|
|
204
|
+
|
|
205
|
+
# Run tests
|
|
206
|
+
pytest
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
## Cross-Language Specification
|
|
210
|
+
|
|
211
|
+
This package implements the [mcp-embedded-ui](https://github.com/aipartnerup/mcp-embedded-ui) specification. The spec repo contains:
|
|
212
|
+
|
|
213
|
+
- [PROTOCOL.md](https://github.com/aipartnerup/mcp-embedded-ui/blob/main/docs/PROTOCOL.md) — endpoint spec, data shapes, security checklist
|
|
214
|
+
- [explorer.html](https://github.com/aipartnerup/mcp-embedded-ui/blob/main/docs/explorer.html) — shared HTML template (identical across all language implementations)
|
|
215
|
+
- [Feature specs](https://github.com/aipartnerup/mcp-embedded-ui/blob/main/docs/features/MANIFEST.md) — detailed requirements and test criteria
|
|
216
|
+
|
|
217
|
+
## License
|
|
218
|
+
|
|
219
|
+
Apache-2.0
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
# mcp-embedded-ui (Python)
|
|
2
|
+
|
|
3
|
+
The Python implementation of [mcp-embedded-ui](https://github.com/aipartnerup/mcp-embedded-ui) — a browser-based tool explorer for any [MCP](https://modelcontextprotocol.io/) (Model Context Protocol) server.
|
|
4
|
+
|
|
5
|
+
## What is this?
|
|
6
|
+
|
|
7
|
+
If you build an MCP server in Python, your users interact with tools through raw JSON — no visual feedback, no schema browser, no quick way to test. This library adds a full browser UI to your server with **one import and one mount**.
|
|
8
|
+
|
|
9
|
+
```
|
|
10
|
+
┌───────────────────────────────────┐
|
|
11
|
+
│ Browser │
|
|
12
|
+
│ Tool list → Schema → Try it │
|
|
13
|
+
└──────────────┬────────────────────┘
|
|
14
|
+
│ HTTP / JSON
|
|
15
|
+
┌──────────────▼────────────────────┐
|
|
16
|
+
│ Your Python MCP Server │
|
|
17
|
+
│ + mcp-embedded-ui │
|
|
18
|
+
│ (FastAPI / Starlette / ASGI) │
|
|
19
|
+
└───────────────────────────────────┘
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## What does the UI provide?
|
|
23
|
+
|
|
24
|
+
- **Tool list** — browse all registered tools with descriptions and annotation badges
|
|
25
|
+
- **Schema inspector** — expand any tool to view its full JSON Schema (`inputSchema`)
|
|
26
|
+
- **Try-it console** — type JSON arguments, execute the tool, see results instantly
|
|
27
|
+
- **cURL export** — copy a ready-made cURL command for any execution
|
|
28
|
+
- **Auth support** — enter a Bearer token in the UI, sent with all requests
|
|
29
|
+
|
|
30
|
+
No build step. No CDN. No external dependencies. The entire UI is a single self-contained HTML page embedded in the package.
|
|
31
|
+
|
|
32
|
+
## Install
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
pip install mcp-embedded-ui
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
Requires Python 3.10+ and [Starlette](https://www.starlette.io/) >= 0.35.
|
|
39
|
+
|
|
40
|
+
## Quick Start
|
|
41
|
+
|
|
42
|
+
### FastAPI / Starlette
|
|
43
|
+
|
|
44
|
+
```python
|
|
45
|
+
from fastapi import FastAPI
|
|
46
|
+
from mcp_embedded_ui import create_mount
|
|
47
|
+
|
|
48
|
+
app = FastAPI()
|
|
49
|
+
|
|
50
|
+
# Mount at /explorer (default)
|
|
51
|
+
app.routes.append(create_mount(tools=my_tools, handle_call=my_handler))
|
|
52
|
+
|
|
53
|
+
# Or specify a custom prefix
|
|
54
|
+
app.routes.append(create_mount("/mcp-ui", tools=my_tools, handle_call=my_handler))
|
|
55
|
+
|
|
56
|
+
# Visit http://localhost:8000/explorer/
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### Any ASGI framework
|
|
60
|
+
|
|
61
|
+
```python
|
|
62
|
+
from mcp_embedded_ui import create_app
|
|
63
|
+
|
|
64
|
+
# Returns a standard ASGI app — mount in any ASGI-compatible framework
|
|
65
|
+
ui_app = create_app(tools=my_tools, handle_call=my_handler)
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
### Full working example
|
|
69
|
+
|
|
70
|
+
```python
|
|
71
|
+
from fastapi import FastAPI
|
|
72
|
+
from mcp_embedded_ui import create_mount
|
|
73
|
+
|
|
74
|
+
# 1. Define your tools (any object with .name, .description, .inputSchema)
|
|
75
|
+
class MyTool:
|
|
76
|
+
def __init__(self, name, description, input_schema):
|
|
77
|
+
self.name = name
|
|
78
|
+
self.description = description
|
|
79
|
+
self.inputSchema = input_schema
|
|
80
|
+
|
|
81
|
+
tools = [
|
|
82
|
+
MyTool("greet", "Say hello", {
|
|
83
|
+
"type": "object",
|
|
84
|
+
"properties": {"name": {"type": "string"}},
|
|
85
|
+
}),
|
|
86
|
+
]
|
|
87
|
+
|
|
88
|
+
# 2. Define a handler: (name, args) -> (content, is_error, trace_id)
|
|
89
|
+
async def handle_call(name, args):
|
|
90
|
+
if name == "greet":
|
|
91
|
+
return [{"type": "text", "text": f"Hello, {args.get('name', 'world')}!"}], False, None
|
|
92
|
+
return [{"type": "text", "text": f"Unknown tool: {name}"}], True, None
|
|
93
|
+
|
|
94
|
+
# 3. Mount the UI
|
|
95
|
+
app = FastAPI()
|
|
96
|
+
app.routes.append(create_mount(tools=tools, handle_call=handle_call))
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
### Dynamic tools
|
|
100
|
+
|
|
101
|
+
```python
|
|
102
|
+
# Sync callable — re-evaluated on every request
|
|
103
|
+
def get_tools():
|
|
104
|
+
return registry.list_tools()
|
|
105
|
+
|
|
106
|
+
# Async callable
|
|
107
|
+
async def get_tools():
|
|
108
|
+
return await registry.async_list_tools()
|
|
109
|
+
|
|
110
|
+
app = create_app(tools=get_tools, handle_call=my_handler)
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
## API
|
|
114
|
+
|
|
115
|
+
### Three-tier API
|
|
116
|
+
|
|
117
|
+
| Function | Returns | Use case |
|
|
118
|
+
|----------|---------|----------|
|
|
119
|
+
| `create_mount(prefix, *, tools, handle_call, **config)` | `Mount` | FastAPI / Starlette — mount under a URL prefix |
|
|
120
|
+
| `create_app(tools, handle_call, **config)` | `ASGIApp` | Any ASGI framework — standalone app |
|
|
121
|
+
| `build_ui_routes(tools, handle_call, **config)` | `list[Route]` | Power users — fine-grained route control |
|
|
122
|
+
|
|
123
|
+
### Parameters
|
|
124
|
+
|
|
125
|
+
| Parameter | Type | Default | Description |
|
|
126
|
+
|-----------|------|---------|-------------|
|
|
127
|
+
| `tools` | `list \| Callable \| AsyncCallable` | _required_ | MCP Tool objects (`.name`, `.description`, `.inputSchema`) |
|
|
128
|
+
| `handle_call` | `ToolCallHandler` | _required_ | `async (name, args) -> (content, is_error, trace_id)` |
|
|
129
|
+
| `allow_execute` | `bool` | `True` | Enable/disable tool execution (enforced server-side) |
|
|
130
|
+
| `auth_hook` | `AuthHook \| None` | `None` | Sync/async context manager factory for auth |
|
|
131
|
+
| `title` | `str` | `"MCP Tool Explorer"` | Page title (HTML-escaped automatically) |
|
|
132
|
+
|
|
133
|
+
### Auth Hook
|
|
134
|
+
|
|
135
|
+
The `auth_hook` receives a Starlette `Request` and returns a context manager (sync or async). Raise inside to reject with 401. The error response is always `{"error": "Unauthorized"}` — internal details are never leaked.
|
|
136
|
+
|
|
137
|
+
```python
|
|
138
|
+
from contextlib import contextmanager
|
|
139
|
+
|
|
140
|
+
@contextmanager
|
|
141
|
+
def my_auth(request):
|
|
142
|
+
token = request.headers.get("Authorization")
|
|
143
|
+
if not valid(token):
|
|
144
|
+
raise ValueError("Bad token")
|
|
145
|
+
my_identity_var.set(decode(token))
|
|
146
|
+
yield
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
Auth only guards `POST /tools/{name}/call`. Discovery endpoints (`GET /tools`, `GET /tools/{name}`, `GET /meta`) are always public.
|
|
150
|
+
|
|
151
|
+
### Endpoints
|
|
152
|
+
|
|
153
|
+
| Method | Path | Description |
|
|
154
|
+
|--------|------|-------------|
|
|
155
|
+
| GET | `/` | Self-contained HTML explorer page |
|
|
156
|
+
| GET | `/meta` | JSON config — `{ title, allow_execute }` |
|
|
157
|
+
| GET | `/tools` | Summary list of all tools |
|
|
158
|
+
| GET | `/tools/{name}` | Full tool detail with `inputSchema` |
|
|
159
|
+
| POST | `/tools/{name}/call` | Execute a tool, returns MCP `CallToolResult` |
|
|
160
|
+
|
|
161
|
+
## Development
|
|
162
|
+
|
|
163
|
+
```bash
|
|
164
|
+
# Install in editable mode with dev dependencies
|
|
165
|
+
pip install -e ".[dev]"
|
|
166
|
+
|
|
167
|
+
# Run the demo
|
|
168
|
+
python examples/fastapi_demo.py
|
|
169
|
+
# Visit http://localhost:8000/explorer/
|
|
170
|
+
|
|
171
|
+
# Run tests
|
|
172
|
+
pytest
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
## Cross-Language Specification
|
|
176
|
+
|
|
177
|
+
This package implements the [mcp-embedded-ui](https://github.com/aipartnerup/mcp-embedded-ui) specification. The spec repo contains:
|
|
178
|
+
|
|
179
|
+
- [PROTOCOL.md](https://github.com/aipartnerup/mcp-embedded-ui/blob/main/docs/PROTOCOL.md) — endpoint spec, data shapes, security checklist
|
|
180
|
+
- [explorer.html](https://github.com/aipartnerup/mcp-embedded-ui/blob/main/docs/explorer.html) — shared HTML template (identical across all language implementations)
|
|
181
|
+
- [Feature specs](https://github.com/aipartnerup/mcp-embedded-ui/blob/main/docs/features/MANIFEST.md) — detailed requirements and test criteria
|
|
182
|
+
|
|
183
|
+
## License
|
|
184
|
+
|
|
185
|
+
Apache-2.0
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import uvicorn
|
|
2
|
+
from fastapi import FastAPI, Request
|
|
3
|
+
from contextlib import contextmanager
|
|
4
|
+
|
|
5
|
+
from mcp_embedded_ui import create_mount
|
|
6
|
+
|
|
7
|
+
app = FastAPI()
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
# Mock MCP tools
|
|
11
|
+
class MockTool:
|
|
12
|
+
def __init__(self, name, description):
|
|
13
|
+
self.name = name
|
|
14
|
+
self.description = description
|
|
15
|
+
self.inputSchema = {
|
|
16
|
+
"type": "object",
|
|
17
|
+
"properties": {
|
|
18
|
+
"message": {"type": "string"},
|
|
19
|
+
},
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
tools = [
|
|
24
|
+
MockTool("echo", "Replies back with your message"),
|
|
25
|
+
MockTool("add", "Add two numbers"),
|
|
26
|
+
]
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
# Tool execution handler
|
|
30
|
+
async def handle_tool_call(name, args):
|
|
31
|
+
if name == "echo":
|
|
32
|
+
return [{"type": "text", "text": f"You said: {args.get('message')}"}], False, "trace-123"
|
|
33
|
+
return [{"type": "text", "text": "Result: 42"}], False, "trace-456"
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
# Auth hook example (use your own ContextVar to propagate identity downstream)
|
|
37
|
+
@contextmanager
|
|
38
|
+
def my_auth_hook(request: Request):
|
|
39
|
+
# token = request.headers.get("Authorization")
|
|
40
|
+
# if not token: raise Exception("Missing token")
|
|
41
|
+
yield # raise here to reject the request with 401
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
# One-liner mount (defaults to /explorer)
|
|
45
|
+
app.routes.append(
|
|
46
|
+
create_mount(
|
|
47
|
+
tools=tools,
|
|
48
|
+
handle_call=handle_tool_call,
|
|
49
|
+
auth_hook=my_auth_hook,
|
|
50
|
+
title="My MCP Explorer",
|
|
51
|
+
)
|
|
52
|
+
)
|
|
53
|
+
|
|
54
|
+
if __name__ == "__main__":
|
|
55
|
+
print("Running MCP Embedded UI at http://localhost:8000/explorer")
|
|
56
|
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "mcp-embedded-ui"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "A lightweight, minimal-dependency embedded Web UI for any MCP Server."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = "Apache-2.0"
|
|
11
|
+
requires-python = ">=3.10"
|
|
12
|
+
authors = [
|
|
13
|
+
{ name = "aipartnerup", email = "team@aipartnerup.com" },
|
|
14
|
+
]
|
|
15
|
+
keywords = ["mcp", "ui", "explorer", "tools", "model-context-protocol", "asgi", "starlette", "fastapi"]
|
|
16
|
+
classifiers = [
|
|
17
|
+
"Development Status :: 3 - Alpha",
|
|
18
|
+
"Intended Audience :: Developers",
|
|
19
|
+
"License :: OSI Approved :: Apache Software License",
|
|
20
|
+
"Programming Language :: Python :: 3",
|
|
21
|
+
"Programming Language :: Python :: 3.10",
|
|
22
|
+
"Programming Language :: Python :: 3.11",
|
|
23
|
+
"Programming Language :: Python :: 3.12",
|
|
24
|
+
"Programming Language :: Python :: 3.13",
|
|
25
|
+
"Topic :: Software Development :: Libraries :: Python Modules",
|
|
26
|
+
"Topic :: Internet :: WWW/HTTP",
|
|
27
|
+
]
|
|
28
|
+
dependencies = [
|
|
29
|
+
"starlette>=0.14.0",
|
|
30
|
+
]
|
|
31
|
+
|
|
32
|
+
[project.optional-dependencies]
|
|
33
|
+
dev = [
|
|
34
|
+
"pytest>=7.0",
|
|
35
|
+
"pytest-cov>=4.0",
|
|
36
|
+
"httpx",
|
|
37
|
+
"mypy>=1.0",
|
|
38
|
+
"ruff>=0.1",
|
|
39
|
+
"fastapi",
|
|
40
|
+
"uvicorn",
|
|
41
|
+
]
|
|
42
|
+
|
|
43
|
+
[project.urls]
|
|
44
|
+
Homepage = "https://aipartnerup.com"
|
|
45
|
+
Documentation = "https://github.com/aipartnerup/mcp-embedded-ui"
|
|
46
|
+
Repository = "https://github.com/aipartnerup/mcp-embedded-ui-python"
|
|
47
|
+
Issues = "https://github.com/aipartnerup/mcp-embedded-ui-python/issues"
|
|
48
|
+
Changelog = "https://github.com/aipartnerup/mcp-embedded-ui-python/blob/main/CHANGELOG.md"
|
|
49
|
+
|
|
50
|
+
[tool.hatch.build.targets.wheel]
|
|
51
|
+
packages = ["src/mcp_embedded_ui"]
|
|
52
|
+
|
|
53
|
+
[tool.pytest.ini_options]
|
|
54
|
+
testpaths = ["tests"]
|
|
55
|
+
asyncio_mode = "strict"
|
|
56
|
+
filterwarnings = ["error"]
|
|
57
|
+
|
|
58
|
+
[tool.mypy]
|
|
59
|
+
python_version = "3.10"
|
|
60
|
+
strict = true
|
|
61
|
+
warn_return_any = true
|
|
62
|
+
warn_unused_configs = true
|
|
63
|
+
|
|
64
|
+
[tool.ruff]
|
|
65
|
+
target-version = "py310"
|
|
66
|
+
line-length = 120
|
|
67
|
+
|
|
68
|
+
[tool.ruff.lint]
|
|
69
|
+
select = ["E", "F", "W", "I", "N", "UP", "B", "SIM"]
|
|
70
|
+
|
|
71
|
+
[tool.coverage.run]
|
|
72
|
+
source = ["src/mcp_embedded_ui"]
|
|
73
|
+
|
|
74
|
+
[tool.coverage.report]
|
|
75
|
+
fail_under = 90
|
|
76
|
+
show_missing = true
|
|
77
|
+
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
|
|
2
|
+
from importlib.metadata import PackageNotFoundError, version
|
|
3
|
+
|
|
4
|
+
from .server import (
|
|
5
|
+
AuthHook,
|
|
6
|
+
CallResult,
|
|
7
|
+
ToolCallHandler,
|
|
8
|
+
ToolsProvider,
|
|
9
|
+
build_mcp_ui_routes,
|
|
10
|
+
build_ui_routes,
|
|
11
|
+
create_app,
|
|
12
|
+
create_mount,
|
|
13
|
+
)
|
|
14
|
+
|
|
15
|
+
try:
|
|
16
|
+
__version__ = version("mcp-embedded-ui")
|
|
17
|
+
except PackageNotFoundError:
|
|
18
|
+
__version__ = "unknown"
|
|
19
|
+
|
|
20
|
+
__all__ = [
|
|
21
|
+
"AuthHook",
|
|
22
|
+
"CallResult",
|
|
23
|
+
"ToolCallHandler",
|
|
24
|
+
"ToolsProvider",
|
|
25
|
+
"build_mcp_ui_routes",
|
|
26
|
+
"build_ui_routes",
|
|
27
|
+
"create_app",
|
|
28
|
+
"create_mount",
|
|
29
|
+
]
|