Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions design/mcp.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,17 @@ command is that handler on a port. Its endpoint is `/mcp`.
The card hands the files over together, the videos' addresses first: the page, given one, asked
the person for the rest while they were on their way. A file that does not come is said by its
name, and the page asks the person for it.
- **A chat that drops fields of `source` sends the source as text** (#346). Gemini's web app
showed a call whose source had its `type` and `title`, and the server got one with neither, the
other fields there (2026-12-03); why is not known. `sourceJson` takes the whole source as JSON
text, and `source`, which stays required, goes with it as `{}`. The text is read only where
`source` came with no `type`: the card plays the source of a call's input where that has a
`type` and otherwise the result's, so the server checks, and the card plays, the same one.
Only a refusal for a source that came with neither field says to use it: the instructions and
the tool's description do not, so a chat that sends `source` whole never writes a source
twice. It is shown and not enforced, as `files` is: one sent empty, null or not as text is
passed over, and text that is no source is refused in the server's own words, a parser's
quoting what it read.
- **The card is the play page's way to the chat, and holds nothing of the activity.**
`lib/card.html` frames `/h5p/play/` of its site, by default its own origin, with the source in
its fragment; the page
Expand Down Expand Up @@ -224,13 +235,13 @@ command is that handler on a port. Its endpoint is `/mcp`.
card's script is one function: a chat puts names of its own on the card's window.
- **Tested as a host runs it.** `tests/app.test.ts` starts the command on a free port and asks it
over HTTP with the SDK's own client: the two tools and their words, the file input's schema,
the card and its policy, a source checked, one refused, attachments handed to the card alone,
the card and its policy, a source checked, one refused, one sent as text, attachments handed to the card alone,
each once, a call whose files are no references, a body over 1 MB, and that nothing was written.
`tests/card.test.ts` runs the card in a browser, the verifier's, in three chats that stand in
for the ones it was written against: one that sandboxes it without an origin, under the policy
Open WebUI writes from the card's domains; one that refuses the frame; one that lets the page
be framed. Each speaks MCP Apps to it, and the play page there says its height and no more:
the source streamed, a stopped call, a result with no input, nothing for a while, a page
the source streamed, a stopped call, a result with no input, a source that came as text, nothing for a while, a page
that never speaks, and attached files the page asks for, matched and handed over together with
the one it does not name left unfetched. `app.test.ts` asks as a chat on protocol 2026-07-28 too, with and without
cards.
Expand Down
43 changes: 40 additions & 3 deletions packages/mcp-app/lib/app.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,37 @@ const FILES = {
}
}

/**
* The source again, as text, which a chat cannot lose a field of (#346): shown as the field's
* schema and not enforced, as `files` is, so a chat that sends it empty or null is answered all the same.
*/
const SOURCE_JSON = {
type: 'string',
description: 'Only where h5p_play said to use it: the whole source as JSON text, with {} as source, for a chat that drops fields of source on the way.'
}

/** Said with a refusal for a source that came with neither field, as Gemini's web app sent one (2026-12-03). */
const AS_TEXT = 'Where the source you sent had its type and title, this chat dropped them on the way: call h5p_play again with the whole source as JSON text in sourceJson, and {} as source.'

/**
* The source a call means: `sourceJson`'s where `source` came with no type, as the card plays the
* input's only with one, so both take the same. Null for text that is no source.
* @param {Record<string, unknown>} source
* @param {unknown} text
* @returns {Record<string, unknown> | null}
*/
function sourceOf(source, text) {
if (source.type || typeof text !== 'string' || !text.trim()) return source
try {
const read = JSON.parse(text)
// A call's arguments written whole, `{"source": {…}}`, are read as the source in them.
const meant = read?.type === undefined && typeof read?.source === 'object' ? read.source : read
return meant && typeof meant === 'object' && !Array.isArray(meant) ? meant : null
} catch {
return null
}
}

/** MCP Apps' name for a chat's cards, and the kind of card this server's is. */
const UI = 'io.modelcontextprotocol/ui'
const CARD_TYPE = 'text/html;profile=mcp-app'
Expand Down Expand Up @@ -186,7 +217,8 @@ export async function createApp(options = {}) {
description: PLAY,
inputSchema: z.object({
source: z.record(z.string(), z.unknown()).meta({ properties: SOURCE_PROPERTIES, description: 'The source, as h5p_types describes it for its type' }),
files: z.unknown().optional().meta(FILES)
files: z.unknown().optional().meta(FILES),
sourceJson: z.unknown().optional().meta(SOURCE_JSON)
}),
annotations: { readOnlyHint: true, destructiveHint: false, openWorldHint: false },
_meta: {
Expand All @@ -196,14 +228,19 @@ export async function createApp(options = {}) {
'openai/toolInvocation/invoking': 'Preparing the activity…',
'openai/toolInvocation/invoked': 'Ready to play'
}
}, async ({ source, files: attachedFiles }) => {
}, async ({ source: sent, files: attachedFiles, sourceJson }) => {
const source = sourceOf(sent, sourceJson)
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
// Its words are not passed on: a parser's message quotes what it read.
if (!source) return refusal('sourceJson is not a source: it takes the whole source, one JSON object, as text. Fix it and call h5p_play again.')
let result
try {
// The author package's check of the source alone: no file is looked for and none
// downloaded, here or anywhere, so a problem names nothing of this computer.
result = await buildPackage(source, undefined, { check: true, unread: true, libraries: options.libraries })
} catch (error) {
if (error instanceof SourceError) return refusal(`${error.message}\nFix each at its path and call h5p_play again.`)
// A source that came as `source` with neither field may have lost them on the way (#346).
const lost = source === sent && !('type' in source) && !('title' in source)
if (error instanceof SourceError) return refusal(`${error.message}\nFix each at its path and call h5p_play again.${lost ? `\n${AS_TEXT}` : ''}`)
// Not a problem of the source's, and its words may be of this computer: none are passed on.
return refusal('The check could not be run, through no fault of the source. Call h5p_play again in a moment.')
}
Expand Down
45 changes: 45 additions & 0 deletions packages/mcp-app/tests/app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,11 @@ describe('the app server', () => {
}
})
expect(play.inputSchema.properties?.file).toBeUndefined()
// The source again as text, asked of no chat: `source` alone is required, as it was (#346).
expect(play.inputSchema.properties?.sourceJson).toEqual({ type: 'string', description: expect.stringMatching(/^Only where h5p_play said to use it/) })
expect(Object.keys(play.inputSchema.properties ?? {})).toEqual(['source', 'files', 'sourceJson'])
expect(play.inputSchema.required).toEqual(['source'])
expect(`${client.getInstructions()}${play.description}${source.description}`).not.toContain('sourceJson')
expect(play._meta).toMatchObject({ 'openai/fileParams': ['files'], 'openai/outputTemplate': expect.stringMatching(/^ui:\/\/h5p-play\/card-[0-9a-f]{12}\.html$/) })
expect((play._meta as any).ui.resourceUri).toBe(play._meta!['openai/outputTemplate'])
// Neither tool keeps or changes anything.
Expand Down Expand Up @@ -237,6 +241,47 @@ describe('the app server', () => {
expect(textOf(draw)).toMatch(/What it holds:\nShows 1 of the 2 questions each time/)
})

it('takes the source as text from a chat that drops its fields on the way', async () => {
// As Gemini's web app sent one (2026-12-03): its type and title gone, the rest there (#346).
const { type: _type, title: _title, ...dropped } = QUIZ
const refused = await call('h5p_play', { source: dropped })
expect(refused.isError).toBe(true)
expect(textOf(refused)).toBe('The source has 2 problems:\n type: expected a content type\'s machine name, such as "H5P.Blanks"\n title: expected the activity\'s title\nFix each at its path and call h5p_play again.\nWhere the source you sent had its type and title, this chat dropped them on the way: call h5p_play again with the whole source as JSON text in sourceJson, and {} as source.')
// The text is read where `source` came with no type, and is what a card drawn from the result plays.
const whole = await call('h5p_play', { source: QUIZ })
for (const source of [{}, dropped]) {
const played = await call('h5p_play', { source, sourceJson: JSON.stringify(QUIZ) })
expect(played.isError).toBeFalsy()
expect(textOf(played)).toBe(textOf(whole))
expect(played._meta?.['h5p/source']).toEqual(QUIZ)
}
// A call's arguments written whole are read as meant, and a problem in the text is said by its path.
expect((await call('h5p_play', { source: {}, sourceJson: JSON.stringify({ source: QUIZ }) }))._meta?.['h5p/source']).toEqual(QUIZ)
const wrong = await call('h5p_play', { source: {}, sourceJson: JSON.stringify({ ...QUIZ, params: { question: 'Q?', correct: 'maybe' } }) })
expect(textOf(wrong)).toMatch(/params\.correct: "maybe" is not an option[\s\S]*call h5p_play again\.$/)
// Text that is no source is refused in the server's own words, with nothing of the text in them.
for (const sourceJson of ['{"type": "H5P.TrueFalse", KEY-IN-THE-TEXT', '[1, 2]', '"H5P.TrueFalse"']) {
const unread = await call('h5p_play', { source: {}, sourceJson })
expect(unread.isError).toBe(true)
expect(textOf(unread)).toBe('sourceJson is not a source: it takes the whole source, one JSON object, as text. Fix it and call h5p_play again.')
}
// A chat that fills an input it does not use, with nothing or with no text, is answered as before.
for (const sourceJson of ['', ' ', null, 0, {}]) {
const played = await call('h5p_play', { source: QUIZ, sourceJson })
expect(textOf(played)).toBe(textOf(whole))
expect(played._meta?.['h5p/source']).toEqual(QUIZ)
}
// A source with its type is the one the card plays from the call's input: checked and answered, whatever the text says.
const other = await call('h5p_play', { source: QUIZ, sourceJson: JSON.stringify({ ...QUIZ, title: 'Another', params: { question: 'Ice floats.', correct: 'true' } }) })
expect(textOf(other)).toBe(textOf(whole))
expect(other._meta?.['h5p/source']).toEqual(QUIZ)
expect((await call('h5p_play', { source: QUIZ, sourceJson: 'no JSON' })).isError).toBeFalsy()
// A source that lacks one field, or that came as text, is told only its problems.
const { title: _only, ...untitled } = QUIZ
expect(textOf(await call('h5p_play', { source: untitled }))).not.toContain('sourceJson')
expect(textOf(await call('h5p_play', { source: {}, sourceJson: JSON.stringify(dropped) }))).not.toContain('sourceJson')
})

it('hands the attached files\' ids to the card, and neither asks their addresses nor passes them on', async () => {
// The store does count whoever asks that address: this test's own asking, here.
await fetch(fileAddress).catch(() => {})
Expand Down
10 changes: 10 additions & 0 deletions packages/mcp-app/tests/card.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ addEventListener('message', (event) => {
else if (input === 'streamed') { part(); setTimeout(part, 400); setTimeout(whole, 1500) }
else if (input === 'stopped') { part(); setTimeout(() => say('ui/notifications/tool-cancelled', { reason: 'The person stopped it' }), 400) }
else if (input === 'result') say('ui/notifications/tool-result', { content: [{ type: 'text', text: 'The activity is checked' }], _meta: { 'h5p/source': ${inScript(SOURCE)} } })
// The source sent as text beside an empty one, as a chat that drops its fields is told to (#346).
else if (input === 'text') { say('ui/notifications/tool-input', { arguments: { source: {}, sourceJson: JSON.stringify(${inScript(SOURCE)}) } }); say('ui/notifications/tool-result', { content: [{ type: 'text', text: 'The activity is checked' }], _meta: { 'h5p/source': ${inScript(SOURCE)} } }) }
else if (input === 'attached') { whole(); say('ui/notifications/tool-result', { content: [{ type: 'text', text: 'The activity is checked' }], _meta: { 'h5p/source': ${inScript(SOURCE)}, 'h5p/files': ${inScript(attachedNow)} } }) }
}
else if (message.method === 'ui/open-link') { window.asked.push(message.params.url); answer(${url.searchParams.get('answer') === 'no' ? "{ error: { code: -32000, message: 'no' } }" : '{ result: {} }'}) }
Expand Down Expand Up @@ -355,6 +357,14 @@ describe('the card, before the activity comes or in its place', () => {
await page.close()
})

it('plays the result\'s source where the input\'s came as text', async () => {
const { page, card } = await open('kind=framed&input=text')
await card.waitForSelector('#page.on')
expect(sourceIn((await card.locator('#page').getAttribute('src'))!)).toEqual(SOURCE)
expect(played).toBe(1)
await page.close()
})

it('says it waits, where nothing comes for a while', async () => {
const { page, card } = await open('kind=framed&input=none')
expect((await said(card)).title).toBe('Opening the activity…')
Expand Down