Reference
MCP
AlterMCP, AlterAuthProvider, AlterContext — FastMCP integration.
The alter_sdk.mcp module integrates with FastMCP so any MCP server can ship as a full OAuth 2.0 Authorization Server backed by Alter’s hosted IDP flow.
Requires the mcp extra:
pip install 'alter-sdk[mcp]'from fastmcp import FastMCPfrom alter_sdk import Appfrom alter_sdk.mcp import AlterMCP, AlterAuthProvider, AlterContext
mcp = FastMCP("my-server")alter_app = App(api_key="alter_rk_…")alter = AlterMCP(alter_app)
@mcp.tool()@alter.tool(provider="provider-id")async def list_resources(ctx: AlterContext, query: str) -> list[dict]: resp = await ctx.request( "GET", "https://api.provider.example/v1/resources", query_params={"q": query}, ) return resp.json()
if __name__ == "__main__": mcp.run()AlterMCP
Section titled “AlterMCP”AlterMCP(vault: App | Agent)Wrapper around an SDK client that exposes the @alter.tool() decorator.
def tool( provider: str, *, label: str | None = None, agent: Agent | None = None,) -> Callable[[F], F]| Parameter | Type | Default | Description |
|---|---|---|---|
provider | str | — | OAuth provider id (e.g. "provider-id"). |
label | str | None | None | Optional sibling-grant label for multi-grant resolution. |
agent | Agent | None | None | Optional Agent whose identity should drive this tool’s requests. Same-Task nesting records the outer agent as parent_agent. |
The decorator:
- Creates an
AlterContextpre-configured with the provider and optional label. - Sets an ambient audit ContextVar so
vault.request()calls inside the tool body pick up the tool name automatically. - Hides the
AlterContextparameter from FastMCP’s generated tool schema. - Catches
GrantNotFoundErrorand returns an MCP error with a fresh Connect URL. - Catches
ScopeReauthRequiredErrorand returns an MCP error with a re-auth hint.
Use app.with_constraints(...) / agent.with_constraints(...) before passing the SDK client to AlterMCP when a tool needs enforceable scope narrowing or a request rule.
from fastmcp import FastMCP
mcp = FastMCP("my-server")
@mcp.tool()@alter.tool(provider="provider-id")async def list_resources(ctx: AlterContext) -> list[dict]: resp = await ctx.request( "GET", "https://api.provider.example/v1/resources", ) return resp.json()AlterContext
Section titled “AlterContext”Per-tool-call request context injected by @alter.tool(). Not constructed directly.
AlterContext( vault: App | Agent, *, provider: str, label: str | None = None, context: dict[str, str] | None = None,)Properties
Section titled “Properties”| Property | Type | Description |
|---|---|---|
provider | str | The OAuth provider id declared on the decorator. |
label | str | None | Optional sibling-grant label declared on the decorator. |
request
Section titled “request”Forwards to the underlying vault.request() with provider= and context= pre-filled. Tool implementations only specify method + URL.
resp = await ctx.request( "GET", "https://api.provider.example/v1/resources", query_params={"q": "example"},)proxy_request
Section titled “proxy_request”Forwards to the underlying vault.proxy_request() with provider=, label=, and context= pre-filled. Use this for restricted grants or HITL / approval-gated calls.
result = await ctx.proxy_request( "POST", "https://api.example.com/v1/actions", json={"approved": True},)AlterAuthProvider
Section titled “AlterAuthProvider”A FastMCP OAuthProvider that turns any MCP server into a full OAuth 2.0 Authorization Server backed by Alter’s IDP flow. Spec-compliant MCP clients can discover, register, authorize, and obtain tokens without mcp-remote or any other shim.
AlterAuthProvider( vault: App | Agent, *, base_url: str, providers: dict[str, list[str]] | None = None,)| Parameter | Type | Default | Description |
|---|---|---|---|
vault | App | Agent | — | SDK client to back the OAuth flow. |
base_url | str | — | Public URL where the MCP server is mounted (e.g. "https://mcp.example.com"). Required for OAuth metadata discovery. |
providers | dict[str, list[str]] | None | None | Map of provider_id → required scopes for the Connect flow. |
from fastmcp import FastMCPfrom alter_sdk import Appfrom alter_sdk.mcp import AlterAuthProvider
vault = App(api_key="alter_rk_…")auth = AlterAuthProvider( vault, base_url="https://mcp.example.com", providers={"provider-id": ["resources:read"]},)
mcp = FastMCP("Resources", auth=auth)Pair with AlterFastAPI.auth_provider() when the same vault should serve both FastAPI and MCP traffic.
AccessToken
Section titled “AccessToken”Re-exported by alter_sdk.mcp so subclass authors can annotate load_access_token / verify_token overrides on AlterAuthProvider subclasses without reaching into FastMCP’s namespace directly.
from alter_sdk.mcp import AccessToken