Skip to content

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:

Terminal window
pip install 'alter-sdk[mcp]'
from fastmcp import FastMCP
from alter_sdk import App
from 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(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]
ParameterTypeDefaultDescription
providerstrOAuth provider id (e.g. "provider-id").
labelstr | NoneNoneOptional sibling-grant label for multi-grant resolution.
agentAgent | NoneNoneOptional Agent whose identity should drive this tool’s requests. Same-Task nesting records the outer agent as parent_agent.

The decorator:

  1. Creates an AlterContext pre-configured with the provider and optional label.
  2. Sets an ambient audit ContextVar so vault.request() calls inside the tool body pick up the tool name automatically.
  3. Hides the AlterContext parameter from FastMCP’s generated tool schema.
  4. Catches GrantNotFoundError and returns an MCP error with a fresh Connect URL.
  5. Catches ScopeReauthRequiredError and 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()

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,
)
PropertyTypeDescription
providerstrThe OAuth provider id declared on the decorator.
labelstr | NoneOptional sibling-grant label declared on the decorator.

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"},
)

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},
)

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,
)
ParameterTypeDefaultDescription
vaultApp | AgentSDK client to back the OAuth flow.
base_urlstrPublic URL where the MCP server is mounted (e.g. "https://mcp.example.com"). Required for OAuth metadata discovery.
providersdict[str, list[str]] | NoneNoneMap of provider_id → required scopes for the Connect flow.
from fastmcp import FastMCP
from alter_sdk import App
from 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.

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

Report an issue with this page

Necessary

Required for sign-in, security, authorization, and remembering your choices.

Always active

Analytics

Helps us understand which product and documentation features are useful.

Performance diagnostics

Uses performance tracing and privacy-masked session replay to diagnose problems.

You can change these choices at any time from Cookie settings.