|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +MCP Apps View Example — render rich UI views in Claude.ai |
| 4 | +
|
| 5 | +This example shows how to create tools that render interactive views |
| 6 | +(charts, maps, tables, etc.) using MCP Apps structured content. |
| 7 | +
|
| 8 | +When Claude.ai calls a tool with `_meta.ui`, it: |
| 9 | +1. Reads the view HTML via `resources/read` on the `resourceUri` |
| 10 | +2. Renders it in an iframe |
| 11 | +3. Passes `structuredContent` as the data payload |
| 12 | +
|
| 13 | +ChukMCPServer handles step 1 automatically — when you register a tool |
| 14 | +with `_meta.ui.resourceUri` (ui:// scheme) and `_meta.ui.viewUrl`, |
| 15 | +the server auto-creates a resource that fetches the HTML from the CDN. |
| 16 | +
|
| 17 | +Requirements: |
| 18 | + pip install chuk-mcp-server httpx |
| 19 | +""" |
| 20 | + |
| 21 | +from chuk_mcp_server import ChukMCPServer |
| 22 | + |
| 23 | +mcp = ChukMCPServer( |
| 24 | + name="view-example", |
| 25 | + version="1.0.0", |
| 26 | + description="MCP Apps view example server", |
| 27 | +) |
| 28 | + |
| 29 | + |
| 30 | +# --------------------------------------------------------------------------- |
| 31 | +# A tool that renders a chart view in Claude.ai |
| 32 | +# --------------------------------------------------------------------------- |
| 33 | +# |
| 34 | +# The `meta` dict tells Claude.ai this tool produces a rich view: |
| 35 | +# - resourceUri: a ui:// URI identifying the view (used for resources/read) |
| 36 | +# - viewUrl: the HTTPS URL serving the view's HTML/JS bundle |
| 37 | +# |
| 38 | +# ChukMCPServer automatically: |
| 39 | +# - Registers a resource at the resourceUri that fetches HTML from viewUrl |
| 40 | +# - Enables the `experimental` capability |
| 41 | +# |
| 42 | +@mcp.tool( |
| 43 | + name="show_chart", |
| 44 | + description="Show programming language popularity as a chart.", |
| 45 | + read_only_hint=True, |
| 46 | + meta={ |
| 47 | + "ui": { |
| 48 | + "resourceUri": "ui://view-example/chart", |
| 49 | + "viewUrl": "https://chuk-mcp-ui-views.fly.dev/chart/v1", |
| 50 | + } |
| 51 | + }, |
| 52 | +) |
| 53 | +async def show_chart(chart_type: str = "bar") -> dict: |
| 54 | + """Render a chart. Returns structured content for the view iframe.""" |
| 55 | + return { |
| 56 | + "content": [{"type": "text", "text": "Programming language popularity chart."}], |
| 57 | + "structuredContent": { |
| 58 | + "type": "chart", |
| 59 | + "version": "1.0", |
| 60 | + "title": "Programming Language Popularity 2025", |
| 61 | + "chartType": chart_type, |
| 62 | + "data": [ |
| 63 | + { |
| 64 | + "label": "Popularity (%)", |
| 65 | + "values": [ |
| 66 | + {"label": "Python", "value": 28.1}, |
| 67 | + {"label": "JavaScript", "value": 21.3}, |
| 68 | + {"label": "TypeScript", "value": 12.7}, |
| 69 | + {"label": "Java", "value": 10.5}, |
| 70 | + {"label": "C#", "value": 7.8}, |
| 71 | + {"label": "Go", "value": 5.2}, |
| 72 | + {"label": "Rust", "value": 3.9}, |
| 73 | + ], |
| 74 | + } |
| 75 | + ], |
| 76 | + }, |
| 77 | + } |
| 78 | + |
| 79 | + |
| 80 | +# --------------------------------------------------------------------------- |
| 81 | +# A tool that renders a markdown view |
| 82 | +# --------------------------------------------------------------------------- |
| 83 | +@mcp.tool( |
| 84 | + name="show_readme", |
| 85 | + description="Show a rich markdown document.", |
| 86 | + read_only_hint=True, |
| 87 | + meta={ |
| 88 | + "ui": { |
| 89 | + "resourceUri": "ui://view-example/markdown", |
| 90 | + "viewUrl": "https://chuk-mcp-ui-views.fly.dev/markdown/v1", |
| 91 | + } |
| 92 | + }, |
| 93 | +) |
| 94 | +async def show_readme() -> dict: |
| 95 | + """Render a markdown document in a rich view.""" |
| 96 | + return { |
| 97 | + "content": [{"type": "text", "text": "Project README document."}], |
| 98 | + "structuredContent": { |
| 99 | + "type": "markdown", |
| 100 | + "version": "1.0", |
| 101 | + "title": "My Project", |
| 102 | + "content": ( |
| 103 | + "# My Project\n\n" |
| 104 | + "A sample project demonstrating **MCP Apps** views.\n\n" |
| 105 | + "## Features\n\n" |
| 106 | + "- Interactive charts\n" |
| 107 | + "- Rich markdown rendering\n" |
| 108 | + "- Data tables with sorting\n\n" |
| 109 | + "```python\n" |
| 110 | + 'mcp.tool(meta={"ui": {...}})\n' |
| 111 | + "```\n" |
| 112 | + ), |
| 113 | + }, |
| 114 | + } |
| 115 | + |
| 116 | + |
| 117 | +if __name__ == "__main__": |
| 118 | + mcp.run() |
0 commit comments