-
Notifications
You must be signed in to change notification settings - Fork 323
feat: support metadata in Python SDK #134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
7e5dd59
5d438a4
9b19f94
abf9ad4
92c9655
b3d50a1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -133,6 +133,110 @@ blob_resource = create_ui_resource({ | |
| }) | ||
| ``` | ||
|
|
||
| ### UI Metadata | ||
|
|
||
| Enhance resources with metadata for client-side handling. The SDK automatically prefixes UI-specific metadata with `mcpui.dev/ui-` to distinguish it from custom metadata. | ||
|
|
||
| #### Preferred Frame Size | ||
|
|
||
| Specify preferred dimensions for UI rendering: | ||
|
|
||
| ```python | ||
| resource = create_ui_resource({ | ||
| "uri": "ui://chart", | ||
| "content": { | ||
| "type": "externalUrl", | ||
| "iframeUrl": "https://charts.example.com/widget" | ||
| }, | ||
| "encoding": "text", | ||
| "uiMetadata": { | ||
| "preferred-frame-size": [800, 600] # width, height in pixels or css units | ||
|
||
| } | ||
| }) | ||
| ``` | ||
|
|
||
| #### Initial Render Data | ||
|
|
||
| Provide data to components at initialization: | ||
|
|
||
| ```python | ||
| resource = create_ui_resource({ | ||
| "uri": "ui://dashboard", | ||
| "content": { | ||
| "type": "remoteDom", | ||
| "script": """ | ||
| function Dashboard({ theme, userId }) { | ||
| // Component receives initial data | ||
| return <div>Dashboard for user {userId}</div>; | ||
| } | ||
| """, | ||
| "framework": "react" | ||
| }, | ||
| "encoding": "text", | ||
| "uiMetadata": { | ||
| "initial-render-data": { | ||
| "theme": "dark", | ||
| "userId": "123" | ||
| } | ||
| } | ||
| }) | ||
| ``` | ||
|
|
||
| #### Multiple Metadata Fields | ||
|
|
||
| Combine multiple metadata fields: | ||
|
|
||
| ```python | ||
| resource = create_ui_resource({ | ||
| "uri": "ui://data-viz", | ||
| "content": { | ||
| "type": "rawHtml", | ||
| "htmlString": "<canvas id='chart'></canvas>" | ||
| }, | ||
| "encoding": "text", | ||
| "uiMetadata": { | ||
| "preferred-frame-size": ["800px", "600px"], | ||
| "initial-render-data": { | ||
| "chartType": "bar", | ||
| "dataSet": "quarterly-sales" | ||
| } | ||
| } | ||
| }) | ||
| ``` | ||
|
|
||
| #### Custom Metadata | ||
|
|
||
| Add custom metadata alongside UI metadata: | ||
|
|
||
| ```python | ||
| resource = create_ui_resource({ | ||
| "uri": "ui://custom-widget", | ||
| "content": { | ||
| "type": "rawHtml", | ||
| "htmlString": "<div>Widget</div>" | ||
| }, | ||
| "encoding": "text", | ||
| "uiMetadata": { | ||
| "preferred-frame-size": [640, 480] | ||
| }, | ||
| "metadata": { | ||
| "customKey": "customValue", | ||
| "version": "1.0.0" | ||
| } | ||
| }) | ||
|
|
||
| # Result includes both prefixed UI metadata and custom metadata: | ||
| # { | ||
| # "resource": { | ||
| # "meta": { | ||
| # "mcpui.dev/ui-preferred-frame-size": [640, 480], | ||
| # "customKey": "customValue", | ||
| # "version": "1.0.0" | ||
| # } | ||
| # } | ||
| # } | ||
| ``` | ||
|
|
||
| ### UI Actions | ||
|
|
||
| Create action results for user interactions: | ||
|
|
@@ -266,7 +370,9 @@ Creates a UI resource from the given options. | |
| { | ||
| "uri": str, # Must start with "ui://" | ||
| "content": Union[RawHtmlPayload, ExternalUrlPayload, RemoteDomPayload], | ||
| "encoding": Literal["text", "blob"] | ||
| "encoding": Literal["text", "blob"], | ||
| "uiMetadata": Optional[dict[str, Any]], # UI-specific metadata (auto-prefixed) | ||
| "metadata": Optional[dict[str, Any]] # Custom metadata | ||
| } | ||
| ``` | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using
UIMetadataKey.PREFERRED_FRAME_SIZEas a dictionary key follows Python best practices, but the comment suggests this can accept CSS unit strings while the constant is defined as a plain string'preferred-frame-size'. Consider documenting the expected value types (e.g., list of strings or list of numbers) in the UIMetadataKey class docstring to clarify the API contract.