Ushka

ushka.api.mcp

Routes as agent tools, over MCP (Model Context Protocol).

Mark the routes an agent may use, declare what each one touches, and serve them:

from ushka.api.mcp import tool

@tool(reads=["orders"], description="Look up an order")
async def get(order_id: int): ...                     # routes/orders/[order_id].py

@tool(writes=["orders"], description="Refund an order")
async def post(order_id: int, reason: str): ...

app.use(MCP(approve=queue))                           # token from USHKA_MCP_TOKEN

Nothing is exposed unless you mark it. A tool call is an ordinary request to your own app (with a service credential from internal_headers if your routes need one), so the security middleware, validation and your Depends (authentication included) all still apply. The agent's arguments are client data like any other: strings arrive Untrusted.

Declared capabilities do two jobs: they become MCP tool annotations the client can show, and they decide approval. A tool that writes, calls something outside, or uses a method other than GET/HEAD needs a human to approve each call; with no approver configured it is refused.

In this module

class ToolSpecWhat a route declares about itself as an agent tool.
def tool()Marks a route handler as an agent tool and declares its capabilities.
class ToolCallWhat a human is asked to approve: exactly what is about to run.
class ApprovalQueueIn-memory approvals for apps that give humans a page or a chat to decide.
def terminal_approver()For local development: asks in the terminal that runs the server.
class MCPServerServes the marked routes of app as MCP tools (JSON-RPC over HTTP POST).
class MCPServes the routes marked with tool to AI agents: app.use(MCP(approve=queue)).

class ToolSpec

What a route declares about itself as an agent tool.

needs_approval

def needs_approval(method: str) -> bool

def tool

def tool(
    func: Callable | None = None,
    *,
    name: str | None = None,
    description: str = '',
    reads: list[str] | tuple[str, ...] = (),
    writes: list[str] | tuple[str, ...] = (),
    calls: list[str] | tuple[str, ...] = (),
    approval: bool | None = None,
)

Marks a route handler as an agent tool and declares its capabilities.

reads and writes name the data it touches ("orders", "db:users"), calls the outside services it reaches ("stripe"). approval=True forces a human approval, False waives it. By default writing, calling out, or a method other than GET requires it.

class ToolCall

What a human is asked to approve: exactly what is about to run.

class ApprovalQueue

In-memory approvals for apps that give humans a page or a chat to decide.

enable_mcp(approve=queue) makes each sensitive call wait here. Your own route (behind your own login!) shows queue.pending() and calls queue.decide(id, True or False). No answer within timeout seconds is a no.

pending

def pending() -> list[ToolCall]

decide

def decide(call_id: str, approved: bool) -> bool

Records the human's answer. False if there is no such pending call.

def terminal_approver

async def terminal_approver(call: ToolCall) -> bool

For local development: asks in the terminal that runs the server.

class MCPServer

Serves the marked routes of app as MCP tools (JSON-RPC over HTTP POST).

handle

async def handle(request) -> tuple[int, Any]

class MCP

Serves the routes marked with tool to AI agents: app.use(MCP(approve=queue)).

Plug it in after your routes are registered. Without a token (token= or USHKA_MCP_TOKEN) it refuses to start unless debug is on; token=False means no shared secret, and the caller's Authorization reaches your routes, which check it. app.enable_mcp() is the same thing.

Parameters

NameTypeDescription
tokenstr

The bearer token agents must send.

approvecallable or ApprovalQueue

Answers, with a human, for each call to a sensitive tool.

pathstr

Where the endpoint lives.

**options

name (server name), instructions (text for the agent) and internal_headers (a dict, or a function of the request: a service credential your routes check).

install

def install(app) -> None