A Python SDK for building MCP UI resources (iframe, panels, text blocks, etc.) in a type-safe way.
Project description
MCP-UI Python SDK
This library is a Python port of the MCP-UI TypeScript SDK.
It provides strongly typed helpers for creating UI resources and UI actions in MCP servers, with good type safety, and MCP-compatible JSON output.
📦 Installation
pip install mcp-ui
🚀 Core Concepts
What is a UI Resource?
A UI resource is a unit of UI data (e.g., an HTML snippet, iframe, or Remote DOM script) that MCP clients can render.
This SDK helps you create them consistently with correct metadata and encodings.
What is a UI Action?
A UI action result represents an action the MCP client should take (e.g., open a link, show a prompt, call a tool).
🔧 Usage
1. Creating a UI Resource
Raw HTML
from mcp_ui import RawHtmlContent, CreateUIResourceOptions, create_ui_resource
options = CreateUIResourceOptions(
uri="ui://demo/html",
content=RawHtmlContent(type="rawHtml", htmlString="<h1>Hello MCP</h1>"),
encoding="text"
)
resource = create_ui_resource(options)
print(resource)
Output:
{
"type": "resource",
"resource": {
"uri": "ui://demo/html",
"mimeType": "text/html",
"text": "<h1>Hello MCP</h1>",
"blob": null,
"_meta": null
}
}
External URL (iframe)
from mcp_ui import ExternalUrlContent, CreateUIResourceOptions, create_ui_resource
options = CreateUIResourceOptions(
uri="ui://demo/frame",
content=ExternalUrlContent(type="externalUrl", iframeUrl="https://example.com"),
encoding="text"
)
iframe_res = create_ui_resource(options)
Output:
{
"type": "resource",
"resource": {
"uri": "ui://demo/frame",
"mimeType": "text/uri-list",
"text": "https://example.com",
"blob": null,
"_meta": null
}
}
Remote DOM (React)
from mcp_ui import RemoteDomContent, CreateUIResourceOptions, create_ui_resource
options = CreateUIResourceOptions(
uri="ui://demo/react",
content=RemoteDomContent(type="remoteDom", script="console.log('Hello')", framework="react"),
encoding="blob"
)
remote_res = create_ui_resource(options)
Output (blob is Base64-encoded):
{
"type": "resource",
"resource": {
"uri": "ui://demo/react",
"mimeType": "application/vnd.mcp-ui.remote-dom+javascript; framework=react",
"blob": "Y29uc29sZS5sb2coJ0hlbGxvJyk=",
"text": null,
"_meta": null
}
}
2. Adding Metadata
You can attach metadata to resources. Keys are automatically prefixed with `mcpui.dev/ui-`.
options = CreateUIResourceOptions(
uri="ui://demo/meta",
content=RawHtmlContent(type="rawHtml", htmlString="<p>Meta Example</p>"),
encoding="text",
uiMetadata={"PREFERRED_FRAME_SIZE": {"width": 500, "height": 300}}
)
meta_res = create_ui_resource(options)
Output includes `_meta`:
{
"type": "resource",
"resource": {
"uri": "ui://demo/meta",
"mimeType": "text/html",
"text": "<p>Meta Example</p>",
"blob": null,
"_meta": {
"mcpui.dev/ui-preferred-frame-size": { "width": 500, "height": 300 }
}
}
}
3. UI Action Results
Tool Call
from mcp_ui import ui_action_result_tool_call
action = ui_action_result_tool_call("searchTool", {"query": "MCP SDK"})
Output:
{
"type": "tool",
"payload": {
"toolName": "searchTool",
"params": { "query": "MCP SDK" }
}
}
Prompt
from mcp_ui import ui_action_result_prompt
action = ui_action_result_prompt("Please confirm your choice")
Output:
{
"type": "prompt",
"payload": { "prompt": "Please confirm your choice" }
}
Link
from mcp_ui import ui_action_result_link
action = ui_action_result_link("https://example.com")
Output:
{
"type": "link",
"payload": { "url": "https://example.com" }
}
Intent
from mcp_ui import ui_action_result_intent
action = ui_action_result_intent("share", {"platform": "twitter"})
Output:
{
"type": "intent",
"payload": {
"intent": "share",
"params": { "platform": "twitter" }
}
}
Notification
from mcp_ui import ui_action_result_notification
action = ui_action_result_notification("Saved successfully!")
Output:
{
"type": "notify",
"payload": { "message": "Saved successfully!" }
}
📖 API Reference
create_ui_resource(options: CreateUIResourceOptions) → Dict[str, Any]
Creates a UI resource for MCP. Returns a JSON-serializable dict.
Parameters:
uri: must start withui://content: one ofRawHtmlContent,ExternalUrlContent,RemoteDomContentencoding:"text"or"blob"uiMetadata: UI-specific metadata (auto-prefixed)metadata: General metadataresourceProps: Extra resource fields
Type System:
- Content payloads:
- RawHtmlContent(htmlString)
- ExternalUrlContent(iframeUrl)
- RemoteDomContent(script, framework)
- Resource encodings:
- HTMLTextContent (text string)
- Base64BlobContent (blob string, base64)
- UI Action Results:
- tool, prompt, link, intent, notify
⚙️ Notes
- Internally uses dataclasses for type safety, but always returns dicts (via `asdict()`) for MCP compatibility.
- Enforces URI format (
ui://prefix). - Auto-encodes blob resources in Base64.
📄 License
MIT – Similar to the original MCP-UI Typescript server SDK.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file mcp_ui-0.1.2.tar.gz.
File metadata
- Download URL: mcp_ui-0.1.2.tar.gz
- Upload date:
- Size: 5.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dbef7b69aa10118806ae6daa6e8aacacaa1729e6a0b2abcaf1efedcc4aa0a00a
|
|
| MD5 |
367c59b577f9602c6bd88dd8edc1ed55
|
|
| BLAKE2b-256 |
c888ea9e85c462a6742e5dada407efe1905744a39cc11cd9038ed25101a002c7
|
File details
Details for the file mcp_ui-0.1.2-py3-none-any.whl.
File metadata
- Download URL: mcp_ui-0.1.2-py3-none-any.whl
- Upload date:
- Size: 6.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1eabbf9898cd19edbfa416b6437e61f68116d20963c8f64136cbb0223214ae48
|
|
| MD5 |
25c3467c1b90c6f6ee21bc9999d1da22
|
|
| BLAKE2b-256 |
ecfac3b9c761a61f45df63490695e1a3ad17a4219c0be0552d4cc1756385eddf
|