Repository navigation
Read MCP Apps tool results from notification params #3679
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
Merged
+142
−42
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
bf951da
Read MCP Apps tool results from notification params
Kludex e39dbe2
Test MCP Apps clock tool-result notifications
Kludex 3950806
Initialize MCP Apps clock views before tool results
Kludex 67cb81a
Make clock notification tests self-contained
Kludex 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
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
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
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,78 @@ | ||
| const assert = require("node:assert/strict"); | ||
| const { execFileSync } = require("node:child_process"); | ||
| const path = require("node:path"); | ||
| const test = require("node:test"); | ||
| const vm = require("node:vm"); | ||
|
|
||
| const root = path.resolve(__dirname, "../.."); | ||
| const python = ` | ||
| import json | ||
|
|
||
| import anyio | ||
|
|
||
| from docs_src.apps import tutorial001 | ||
| from mcp import Client | ||
| from mcp.client import advertise | ||
| from mcp.server.apps import APP_MIME_TYPE, EXTENSION_ID | ||
| from mcp.types import TextResourceContents | ||
| from stories.apps.server import RESOURCE_URI, build_server | ||
|
|
||
|
|
||
| async def main() -> None: | ||
| cases = [] | ||
| for name, server, uri in ( | ||
| ("tutorial", tutorial001.mcp, "ui://clock/app.html"), | ||
| ("story", build_server(), RESOURCE_URI), | ||
| ): | ||
| async with Client(server, extensions=[advertise(EXTENSION_ID, {"mimeTypes": [APP_MIME_TYPE]})]) as client: | ||
| resource = await client.read_resource(uri) | ||
| result = await client.call_tool("get_time", {}) | ||
| content = resource.contents[0] | ||
| assert isinstance(content, TextResourceContents) | ||
| assert content.mime_type == APP_MIME_TYPE | ||
| cases.append({"name": name, "html": content.text, "result": result.model_dump(mode="json", by_alias=True)}) | ||
| print(json.dumps(cases)) | ||
|
|
||
|
|
||
| anyio.run(main) | ||
| `; | ||
|
|
||
| const cases = JSON.parse( | ||
| execFileSync("uv", ["run", "--frozen", "--no-sync", "python", "-c", python], { | ||
| cwd: root, | ||
| encoding: "utf8", | ||
| env: { | ||
| ...process.env, | ||
| PYTHONPATH: [root, path.join(root, "examples"), process.env.PYTHONPATH] | ||
| .filter(Boolean) | ||
| .join(path.delimiter), | ||
| }, | ||
| }), | ||
| ); | ||
|
Kludex marked this conversation as resolved.
Resolved
|
||
|
|
||
|
Kludex marked this conversation as resolved.
Resolved
|
||
| for (const { name, html, result } of cases) { | ||
| test(`${name} clock shows the tool-result notification`, () => { | ||
| const script = html.match(/<script>([\s\S]*?)<\/script>/)?.[1]; | ||
| assert.ok(script); | ||
| const clock = { textContent: "..." }; | ||
| let onMessage; | ||
| vm.runInNewContext(script, { | ||
| window: { | ||
| addEventListener(type, handler) { | ||
| assert.equal(type, "message"); | ||
| onMessage = handler; | ||
| }, | ||
| }, | ||
| document: { | ||
| getElementById(id) { | ||
| assert.equal(id, "now"); | ||
| return clock; | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| assert.equal(typeof onMessage, "function"); | ||
| onMessage({ data: { jsonrpc: "2.0", method: "ui/notifications/tool-result", params: result } }); | ||
| assert.equal(clock.textContent, result.content[0].text); | ||
| }); | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.