-
Notifications
You must be signed in to change notification settings - Fork 25
feat: HTML widget support (MCP Apps UI) #490
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
Draft
corinagum
wants to merge
16
commits into
main
Choose a base branch
from
cg/widgets
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
e049601
Invoke
corinagum 88ed1fe
HTML Widgets
corinagum 4daa4a9
Fix to use MessageActivityInput
corinagum 6bfe2f7
Add HTML Widgets example
corinagum 86c03aa
Import fix
corinagum 633c590
uv.lock
corinagum 4e9e41c
Add ExperimentalTeamsHtmlWidget preview markers
corinagum 583a2d4
Align docstring markers to use Diagnostic: ExperimentalTeamsHtmlWidget
corinagum 28f4966
Fix CI: use requires-python >=3.11, move imports to top, ruff format
corinagum 495bd3c
Fix pyright errors in example: use typed models and Any for args
corinagum f6a4121
Address Copilot review feedback
corinagum abf63d0
Fix script injection: escape </script> and newlines in widget name/ve…
corinagum 3521459
Integration tests
corinagum c1940e6
Regex fix
corinagum 8bb477b
Address feedback
corinagum 030d5e4
Fix McpUiCallToolResultContent usage
corinagum File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| # HTML Widgets Example | ||
|
|
||
| This example demonstrates the full HTML widget capabilities for Teams bots using the Python SDK. | ||
|
|
||
| ## Commands | ||
|
|
||
| | Command | Description | | ||
| |---------|-------------| | ||
| | `/simple` | Static widget (no callbacks) | | ||
| | `/calltool` | Widget with onCallTool (tools/call) | | ||
| | `/messageback` | Widget with onMessage (messageBack) | | ||
| | `/fullscreen` | Widget requesting fullscreen display mode | | ||
| | `/multi` | Widget with multiple tools (getTime, roll, echo) | | ||
| | `/openlink` | Widget with ui/open-link | | ||
| | `/context` | Widget with ui/update-model-context | | ||
| | `/hostcontext` | Inspect hostContext from ui/initialize | | ||
| | `/validate` | Security policy validation demo | | ||
| | `/help` | List available commands | | ||
|
|
||
| ## Architecture | ||
|
|
||
| ``` | ||
| src/ | ||
| main.py # Bot entry point, command routing, callTool handler | ||
| widgets/ | ||
| __init__.py # Re-exports all widget HTML constants | ||
| simple.py # Static widget HTML | ||
| calltool.py # CallTool interactive widget HTML | ||
| fullscreen.py # Fullscreen request widget HTML | ||
| messageback.py # MessageBack widget HTML | ||
| multi_tool.py # Multi-tool dispatch widget HTML | ||
| open_link.py # Open link widget HTML | ||
| update_context.py # Update model context widget HTML | ||
| host_context.py # Host context inspector widget HTML | ||
| ``` | ||
|
|
||
| ## Running | ||
|
|
||
| ```bash | ||
| # From the repo root | ||
| cd examples/html-widgets | ||
| cp ../../.env .env # Or create .env with CLIENT_ID, CLIENT_SECRET, TENANT_ID | ||
|
|
||
| # Install dependencies (uses workspace) | ||
| uv sync | ||
|
|
||
| # Start the bot | ||
| uv run python src/main.py | ||
| ``` | ||
|
|
||
| ## Widget Response Format | ||
|
|
||
| The `on_widget_call_tool` handler returns an `HtmlWidgetCallToolResponse`: | ||
|
|
||
| ```python | ||
| @app.on_widget_call_tool | ||
| async def handle(ctx): | ||
| return HtmlWidgetCallToolResponse( | ||
| response_type="htmlwidget/calltoolresult", | ||
| call_tool_result=McpUiCallToolResult( | ||
| content=[{"type": "text", "text": "Result"}], | ||
| structured_content={"key": "value"}, | ||
| is_error=False, | ||
| ), | ||
| ) | ||
| ``` | ||
|
|
||
| ## Key Concepts | ||
|
|
||
| - **`build_html_widget_message`**: Builds a complete message activity with the widget, including protocol injection and security policy defaults. | ||
| - **`build_html_widget_markdown`**: Lower-level helper that returns the markdown string (for embedding in custom activities). | ||
| - **`validate_security_policy`**: Dev-time audit tool that checks HTML for external references not covered by the security policy. | ||
| - **`inject_widget_protocol`**: Auto-injected by the builders. Handles the ui/initialize handshake, size reporting, and optional notification hooks. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| [project] | ||
| name = "html-widgets" | ||
| version = "0.1.0" | ||
| description = "HTML Widgets example bot" | ||
| readme = "README.md" | ||
| requires-python = ">=3.11,<4.0" | ||
| dependencies = [ | ||
| "dotenv>=0.9.9", | ||
| "microsoft-teams-apps", | ||
| "microsoft-teams-api", | ||
| ] | ||
|
|
||
| [tool.uv.sources] | ||
| microsoft-teams-apps = { workspace = true } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
is it possible to show via a GIF or more visuals to showcase what all these widgets look like?