Reference · Tools for AI clients

mcp_token

signature
mcp_token(map | null=) -> str

What it does

Makes a token for the program's MCP door over HTTP, /_quillo/mcp, and gives it once. The person signed in pastes it into their AI client, which sends it with each call as Authorization: Bearer <token>. It is called in a route, where someone is signed in, and the token is tied to that session: in a tool called with it, session() and tenant() are those of the session it was made in, as they were then. It lasts as long as the session has left when the program gave the session a max_age, else 30 days; the option max_age (seconds, or a duration such as "7d") says otherwise, up to a year. The option user names the caller for a program that does not keep it as session().user. It ends with revoke_mcp_token, or with end_session() in the session that made it. See Tools for AI clients.

Examples

in a program
/// The caller's notes, newest first.fn mcp my_notes() -> list with db =    db.query("select id, text from notes where owner = ? order by id desc", session().user)route POST /settings/ai/token {    if session().user == null { reply 401 "Sign in first" }    let token = mcp_token({ max_age: "7d" })    <p>Paste this into your AI client. It is shown only now: <code>{token}</code></p>}

The page shows a token such as qmcp_aa1WdmIllJrQsAomA-gEYfOsTRJcK_jjUZ83ydloPpY, and a client that sends it calls my_notes as the person who made it. The route is a POST, a button in a form, so that no link or prefetch makes a token.

Outside a request there is no session to tie a token to:

this one fails
print(mcp_token())
output
line 1: `mcp_token` is made while a request is answered, where someone is signed in: the token stands for that session

Edge cases

Errors

Each of these stops the request, with its message in the log:

Chaining

A settings page usually makes one, lists them with mcp_tokens(), and ends one with revoke_mcp_token:

in a program
route GET /settings/ai {    if session().user == null { reply 401 "Sign in first" }    <div>        <form method="post" action="/settings/ai/token"><button>Make a token</button></form>        <ul>{for t in mcp_tokens() =>            <li key={t.id}>Good until {date.format(date.from_ms(t.expires), "YYYY-MM-DD")}                <form method="post" action="/settings/ai/revoke">                    <input type="hidden" name="id" value={t.id}/><button>End it</button>                </form>            </li>}</ul>    </div>}route POST /settings/ai/revoke { form: { id: str } } {    revoke_mcp_token(id)    redirect("/settings/ai")}

See also

mcp_tokens, revoke_mcp_token, end_session, session, Tools for AI clients

Thread
1Useful? Like it, or add to its thread below.

Thread 0 posts

On mcp_token(): questions, tips, corrections, and code that uses it.

No posts yet. The first one opens the thread.

    Edit this page on GitHub · every example's output was made by running it (tools/refcheck.mjs) · questions on Discord or r/quillo