Skip to content

Add tool result _meta in tool response and access in the ui#677

Open
zekebergida wants to merge 1 commit into
modelcontextprotocol:mainfrom
zekebergida:toolOutput-meta-example
Open

Add tool result _meta in tool response and access in the ui#677
zekebergida wants to merge 1 commit into
modelcontextprotocol:mainfrom
zekebergida:toolOutput-meta-example

Conversation

@zekebergida

Copy link
Copy Markdown

Include _meta in tool output and access it from the ui

Motivation and Context

While the docs specify that tooloutput _meta is forwarded to the ui and hidden from the model some providers including the basic host in this repo fail to make the _meta available to the ui. This PR will help demonstrate the functionality and can help others troubleshoot this.

How Has This Been Tested?

Claude.ai does forward tool output meta to the ui however chatgpt does not and there are ongoing discussions in developer communities for how to troubleshoot this

Breaking Changes

No

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Additional context

// Handle tool results from the server. Set before `app.connect()` to avoid
// missing the initial tool result.
app.ontoolresult = (result) => {
console.info("tool-result _meta:", (result as { _meta?: unknown })._meta);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove console.logs

getTimeBtn.addEventListener("click", async () => {
// `app.callServerTool()` lets the UI request fresh data from the server
const result = await app.callServerTool({ name: "get-time", arguments: {} });
console.info("callServerTool _meta:", (result as { _meta?: unknown })._meta);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove console.logs

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the quickstart MCP App example to demonstrate that tool result _meta is forwarded to the UI (per the Apps spec) by emitting _meta from the server tool and rendering a selected _meta field in the view.

Changes:

  • Add _meta to the get-time tool result in the quickstart server.
  • Display/log tool-result _meta in the quickstart UI (both via ontoolresult and callServerTool()).
  • Add a small UI element to show the _meta.source value.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.

File Description
examples/quickstart/src/mcp-app.ts Reads and displays result._meta in the UI; adds logging for _meta.
examples/quickstart/server.ts Adds _meta fields to the tool result returned by get-time.
examples/quickstart/mcp-app.html Adds a Tool Meta Source field to the quickstart UI.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +11 to +14
function extractMetaSource(result: unknown): string {
const meta = (result as { _meta?: { source?: unknown } })._meta;
return typeof meta?.source === "string" ? meta.source : "[missing]";
}
Comment on lines 18 to 23
app.ontoolresult = (result) => {
console.info("tool-result _meta:", (result as { _meta?: unknown })._meta);
const time = result.content?.find((c) => c.type === "text")?.text;
serverTimeEl.textContent = time ?? "[ERROR]";
toolMetaSourceEl.textContent = extractMetaSource(result);
};
Comment on lines 28 to +32
const result = await app.callServerTool({ name: "get-time", arguments: {} });
console.info("callServerTool _meta:", (result as { _meta?: unknown })._meta);
const time = result.content?.find((c) => c.type === "text")?.text;
serverTimeEl.textContent = time ?? "[ERROR]";
toolMetaSourceEl.textContent = extractMetaSource(result);
Comment on lines +40 to +44
_meta: {
timestamp: time,
source: "quickstart-server",
debugToken: "quickstart-meta-debug",
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants