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
12 changes: 8 additions & 4 deletions design/mcp.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,10 +229,14 @@ command is that handler on a port. Its endpoint is `/mcp`.
by a stand-in card that hands it a file, and in the card itself; in ChatGPT itself ran the
probe's card, which this one is without its test parts.

- **The card names an origin of its own only where a host gives one** (#307), as `domain`: ChatGPT
asks for one to submit an app (`_meta.ui.domain`, an origin; `openai/widgetDomain` is its alias),
and MCP Apps leaves the value's form to each chat, so no default suits them all. The card's
address takes it into its hash.
- **The card names an origin of its own in each chat's form** (#307, #329). MCP Apps leaves the
form of `_meta.ui.domain` to each chat. ChatGPT's is `domain`, an origin it asks for to submit
an app, written as `openai/widgetDomain`. Claude's, in `_meta.ui.domain`, is the first 32 hex
of the SHA-256 of the connector's address with `.claudemcpcontent.com`, computed from the
address each request was sent to (`/mcp`, the only path the server answers): Claude shows an
error in place of a card whose `ui.domain` is not that, and lets a site framed in the card use
`localStorage` only with one. The card's address hashes the card and its policy, Claude's origin
aside, which differs by address and not by card.
- **A host runs `createApp()`, the package's main export, with its own site and name.** It takes
the play page's `site`, the `build` page a chat with no card is sent to, missing-elements.github.io's
by default, which the card may open too, and the server's `title`; and, for a bundle such as a Cloudflare Worker, what
Expand Down
5 changes: 3 additions & 2 deletions packages/mcp-app/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ Its endpoint is `/mcp`, with no sign-in, and the play page is at `/h5p/play/`. I
- `--build`: the build page a chat with no card is sent to,
[missing-elements.github.io's](https://missing-elements.github.io/h5p/build/) by default.
- `--title`: the name the server gives itself.
- `--domain`: the card's own origin, in the chat's own form. ChatGPT takes an origin, as
`https://lessons.example`, and asks for one to submit an app. None by default.
- `--domain`: the card's own origin in ChatGPT, as `https://lessons.example`, which it asks for
to submit an app. None by default. Claude's is computed from the address it calls, the
connector's, so a proxy in front of the server must pass on the public host.

It has two tools: `h5p_types`, which lists the content types and says how to write each one, and
`h5p_play`, which checks an activity's source and shows the card.
Expand Down
5 changes: 3 additions & 2 deletions packages/mcp-app/bin/h5p-mcp-app.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ Options:
https://missing-elements.github.io/h5p/build/ by default
--title <name> the name the server gives itself in a chat;
H5P Author MCP by Missing Elements by default
--domain <origin> the card's own origin, in the chat's own form: ChatGPT takes an origin,
as https://example.org, and asks for one to submit an app; none by default
--domain <origin> the card's own origin in ChatGPT, as https://example.org, which it asks
for to submit an app; none by default. Claude's is computed from the
address it calls
--version
-h, --help
`
Expand Down
53 changes: 36 additions & 17 deletions packages/mcp-app/lib/app.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { SOURCE_PROPERTIES, TYPES, answer, heldLines, refusal, skillCatalog, typ
* @typedef {object} AppOptions
* @property {string} [site] the origin of the site whose play page the card frames; by default the origin each request was sent to, where the command serves the page
* @property {string} [build] the build page's address, which a chat with no card hands the source to and the card may open; missing-elements.github.io's by default
* @property {string} [domain] the card's own origin, in the chat's own form (#307): ChatGPT takes an origin, as `https://example.org`, and asks for one to submit an app; none by default
* @property {string} [domain] the card's own origin in ChatGPT (#307), as `https://example.org`, which it asks for to submit an app; none by default. Claude's is computed from the address it calls (#329)
* @property {string} [title] the name the server gives itself, which a chat shows; `H5P Author MCP by Missing Elements` by default
* @property {string} [libraries] the pack's `parts/` folder to check with; the installed pack by default
* @property {string} [references] the folder of the author package's type files and brief, its `references/`, with the skill's `SKILL.md` and `LICENSE` beside it, as in the package; the installed package's by default
Expand Down Expand Up @@ -80,6 +80,13 @@ const handOver = (build, files) => `hand the activity over to be built in the pe
/** Where a chat keeps attached files, which the card asks from the person's browser: ChatGPT's, as seen in November 2026. */
const FILE_HOSTS = ['https://*.blob.core.windows.net', 'https://files.oaiusercontent.com', 'https://*.oaiusercontent.com']

/**
* The card's own origin in Claude's form (#329): the first 32 hex of the SHA-256 of the connector's
* address, as configured in Claude, which is the address Claude sends its requests to.
* @param {string} connector
*/
const claudeDomain = (connector) => `${createHash('sha256').update(connector).digest('hex').slice(0, 32)}.claudemcpcontent.com`

/**
* What of the chat's reference to an attached file goes to the card: its id, and its name and type
* where they came. Not its address, which lets its holder read the file and a call could make up.
Expand Down Expand Up @@ -123,7 +130,8 @@ export async function createApp(options = {}) {
// The build page is missing-elements.github.io's, wherever the play page is (#299).
const build = web(options.build ?? BUILD, 'build').href
const title = options.title ?? TITLE
// Each chat reads it its own way, so its form is not checked here beyond being one word.
// ChatGPT's, as `openai/widgetDomain`: Claude's is computed from the address it calls (#329). The form
// is ChatGPT's to check, so here it is only one word.
const domain = options.domain
if (domain !== undefined && !/^\S{1,255}$/.test(domain)) throw new Error(`domain is not a card's origin, as https://example.org: ${domain}`)
// A Worker's bundle (#272) has no package folder for `import.meta` to find: it passes `version` and `card`.
Expand All @@ -137,22 +145,27 @@ export async function createApp(options = {}) {
const skills = await skillCatalog({ references: options.references })

/**
* The card, its policy and the MCP handler for the site whose play page the card frames.
* The card, its policy and the MCP handler for the site whose play page the card frames, as
* asked at the connector's address.
* @param {string} site
* @param {string} connector
*/
function forSite(site) {
function forSite(site, connector) {
const card = cardText.replaceAll('__SITE__', site).replaceAll('__BUILD__', new URL(build).origin)
// A chat keeps a card by its address, so a card that changed has another, and so has one
// given another origin of its own.
const cardUri = `ui://h5p-play/card-${createHash('sha256').update(card).update(domain ?? '').digest('hex').slice(0, 12)}.html`
// The card's policy: the one site it frames, and the chat's file store, which it asks for the
// attached file. Written twice, as the MCP Apps specification places it and as ChatGPT reads it.
const cardMeta = {
ui: { csp: { frameDomains: [site], connectDomains: FILE_HOSTS, resourceDomains: FILE_HOSTS }, prefersBorder: true, ...(domain ? { domain } : {}) },
const policy = {
ui: { csp: { frameDomains: [site], connectDomains: FILE_HOSTS, resourceDomains: FILE_HOSTS }, prefersBorder: true },
...(domain ? { 'openai/widgetDomain': domain } : {}),
'openai/widgetCSP': { frame_domains: [site], connect_domains: FILE_HOSTS, resource_domains: FILE_HOSTS, redirect_domains: [...new Set([site, new URL(build).origin])] },
'openai/widgetDescription': 'Plays the H5P activity, built in the person\'s browser, with its download.'
}
// A chat keeps a card by its address, policy and all, so a card or a policy that changed has
// another. Claude's origin is left out: it differs by the address a chat calls, not by the card.
const cardUri = `ui://h5p-play/card-${createHash('sha256').update(card).update(JSON.stringify(policy)).digest('hex').slice(0, 12)}.html`
// Claude shows an error in place of a card whose origin is not this; with it, the play page
// framed in the card may keep storage (#329).
const cardMeta = { ...policy, ui: { ...policy.ui, domain: claudeDomain(connector) } }

function server() {
const mcp = new McpServer({ name: 'h5p-mcp-app', title, version }, { instructions: INSTRUCTIONS })
Expand Down Expand Up @@ -252,12 +265,16 @@ export async function createApp(options = {}) {

/** @type {Map<string, ReturnType<typeof forSite>>} */
const sites = new Map()
/** @param {string} site */
const held = (site) => {
let found = sites.get(site)
/**
* @param {string} site
* @param {string} connector
*/
const held = (site, connector) => {
const key = `${site} ${connector}`
let found = sites.get(key)
if (!found) {
found = forSite(site)
sites.set(site, found)
found = forSite(site, connector)
sites.set(key, found)
// Dropped, not closed: a request still running on it finishes.
for (const oldest of sites.keys()) {
if (sites.size <= SITES_HELD) break
Expand All @@ -269,19 +286,21 @@ export async function createApp(options = {}) {

return {
version,
cardUri: fixed === null ? undefined : held(fixed).cardUri,
// The same at every address: Claude's origin is not in it.
cardUri: fixed === null ? undefined : held(fixed, `${fixed}/mcp`).cardUri,
/**
* @param {Request} request
* @returns {Promise<Response>}
*/
async fetch(request) {
const { pathname } = new URL(request.url)
const { origin, pathname, search } = new URL(request.url)
// For whoever opens the address, and for a host that asks whether the server is up.
if (pathname === '/' && (request.method === 'GET' || request.method === 'HEAD')) return new Response(`${title}: the MCP endpoint is /mcp\n`, { headers: { 'content-type': 'text/plain; charset=utf-8' } })
if (pathname !== '/mcp') return new Response('Not found\n', { status: 404, headers: { 'content-type': 'text/plain; charset=utf-8' } })
// Said before a body is read, where its length is given; the handler holds one that gives none to the same bound.
if (Number(request.headers.get('content-length')) > MOST) return new Response('A request takes 1 MB at most\n', { status: 413, headers: { 'content-type': 'text/plain; charset=utf-8' } })
return held(fixed ?? new URL(request.url).origin).handler.fetch(request)
// The connector's address is the one asked, query and all, as Claude hashes it.
return held(fixed ?? origin, `${origin}/mcp${search}`).handler.fetch(request)
},
close: async () => { await Promise.all([...sites.values()].map(({ handler }) => handler.close())) }
}
Expand Down
23 changes: 18 additions & 5 deletions packages/mcp-app/tests/app.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { spawn, spawnSync, type ChildProcess } from 'node:child_process'
import { createHash } from 'node:crypto'
import { mkdir, mkdtemp, readdir, readFile, rm, symlink, writeFile } from 'node:fs/promises'
import { request as httpRequest } from 'node:http'
import { createServer, type AddressInfo, type Server } from 'node:net'
Expand All @@ -16,7 +17,9 @@ import { createApp } from '../lib/app.mjs'
*/

const COMMAND = resolve(import.meta.dirname, '../bin/h5p-mcp-app.mjs')
const META = { language: 'en', license: 'U', authors: [{ name: 'Unknown', role: 'Author' }] }
/** The card's own origin in Claude's form, for a connector's address (#329). */
const claude = (connector: string) => `${createHash('sha256').update(connector).digest('hex').slice(0, 32)}.claudemcpcontent.com`
const META ={ language: 'en', license: 'U', authors: [{ name: 'Unknown', role: 'Author' }] }
const QUIZ = { ...META, type: 'H5P.TrueFalse', title: 'Boiling point', params: { question: '<p>Water boils at 100 °C at sea level.</p>', correct: 'true' } }
const VIDEO = {
...META,
Expand Down Expand Up @@ -330,7 +333,7 @@ describe('the app server', () => {
await expect(createApp({ site: 'data:text/html,<p>x</p>' })).rejects.toThrow(/site is not a web address/)
})

it('names the card\'s own origin only where a host gives one, under both names, in the listing and on the card', async () => {
it('names the card\'s own origin in each chat\'s form, in the listing and on the card', async () => {
const cardOf = async (options: Parameters<typeof createApp>[0]) => {
const app = await createApp({ site: 'https://lesson.example', ...options })
const ask = async (method: string, params: unknown) => {
Expand All @@ -344,14 +347,16 @@ describe('the app server', () => {
await app.close()
return { uri: app.cardUri, metas: [listed, read] }
}
// Claude's, from the address it asked at: Claude shows an error in place of a card with another (#329).
const none = await cardOf({})
for (const meta of none.metas) {
expect(meta.ui.domain).toBeUndefined()
expect(meta.ui.domain).toBe(claude('https://lesson.example/mcp'))
expect(meta).not.toHaveProperty('openai/widgetDomain')
}
// ChatGPT's where a host gives one, under ChatGPT's own name, beside Claude's.
const named = await cardOf({ domain: 'https://lesson.example' })
for (const meta of named.metas) {
expect(meta.ui.domain).toBe('https://lesson.example')
expect(meta.ui.domain).toBe(claude('https://lesson.example/mcp'))
expect(meta['openai/widgetDomain']).toBe('https://lesson.example')
}
// A chat keeps a card by its address: a card given an origin has another.
Expand All @@ -376,6 +381,12 @@ describe('the app server', () => {
expect(one.card.text).toContain("const SITE = 'https://lesson.example'")
expect(one.card._meta.ui.csp.frameDomains).toEqual(['https://lesson.example'])
expect((await cardAt('https://other.example/mcp')).uri).not.toBe(one.uri)
// Claude's origin for the card, as Claude's documentation computes it for this address.
expect((await cardAt('https://example.com/mcp')).card._meta.ui.domain).toBe('c3d80a4ed901ee05b21755a88273b4a4.claudemcpcontent.com')
// Another connector's address for the same site: Claude's origin differs, the card's address does not.
const queried = await cardAt('https://lesson.example/mcp?team=1')
expect(queried.card._meta.ui.domain).not.toBe(one.card._meta.ui.domain)
expect(queried.uri).toBe(one.uri)
await app.close()
// The command takes the scheme a proxy that ends TLS says, and the host the request names.
const card = await new Promise<string>((done, failed) => {
Expand Down Expand Up @@ -408,7 +419,9 @@ describe('the app server', () => {
return JSON.parse(text.split('\n').find((line) => line.startsWith('data: '))?.slice(6) ?? text).result
}
const uri = (await ask('tools/list', {})).tools.find((tool: { name: string }) => tool.name === 'h5p_play')._meta['openai/outputTemplate']
expect((await ask('resources/read', { uri })).contents[0]._meta.ui.domain).toBe('https://card.example')
const meta = (await ask('resources/read', { uri })).contents[0]._meta
expect(meta['openai/widgetDomain']).toBe('https://card.example')
expect(meta.ui.domain).toBe(claude(`${at}/mcp`))
} finally {
other.kill()
}
Expand Down