Skip to content

Allow parent request handler to initialize context variables / state / env #104

@brettimus

Description

@brettimus

Sometimes you have information that you want to pass down to tools via context.

Currently, there is no way for a parent request to initialize context for the server, like this:

app.all("/mcp", async (ctx) => {
    const db = ctx.get("db");
    const response = await httpHandler(ctx.req.raw, { state: { db } });
    return response;
})

Such that tools have access to an underlying database connection:

mcp.tool("my-tool", {
    handler: (args, ctx) => {
        // ... do something with ctx.state.db
    },
})

We will need to employ some optimistic typing tricks similar to Hono to make this happen.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions