The library¶
For an MCP server of your own, in Python, that needs more than a config file: hand-written tools beside the generated ones, a download that returns a file, a sign-in that mints a key for the person.
uv add git+https://github.com/Marceltov/sesamcp --tag v0.1.0
A server from a spec¶
import os
from sesamcp.server import build_server, resolve_auth_mode, serve
from sesamcp.tools.spec import SpecSource, resolve_spec
spec, origin = resolve_spec(SpecSource(url="http://pets:8000/openapi.json"))
mcp = build_server(
name="Pets MCP", service_name="Pets", spec=spec, api_url="http://pets:8000/api",
headers_for=lambda grant: {"Authorization": f"Bearer {grant['api_key']}"},
static_variable="api_key", # token mode: the raw header is the key
toolsets={"pets", "owners"}, never_exposed=("/admin",),
guides={"listPets": "Start here."},
annotation_sets={"read_only": {"searchPets"}, "open_world": {"importPet"}},
)
serve(mcp, resolve_auth_mode(os.environ))
| Part | Where | Does |
|---|---|---|
resolve_spec |
sesamcp.tools.spec |
a URL, a file, or the app's version filled into a release URL, with a bundled fallback |
select_operations |
sesamcp.tools.select |
toolsets, never_exposed, replaced (your own tools take the path), PUT dropped where PATCH exists, union bodies flattened |
describer |
sesamcp.tools.describe |
GET /path first, your guide or the spec's text, read-only/destructive/idempotent hints, no output schema |
refusal_hook |
sesamcp.tools.refusal |
the app's 401 as "sign in again", its 403 as "your login lacks this permission" |
GrantAuth |
sesamcp.tools.auth |
every call carries headers_for(grant); the client's own headers never reach the app |
build_error_server |
sesamcp.server |
a stand-in that explains a failed start over a live connection |
extra=lambda mcp, client: ... adds hand-written tools that share the same client, and so the same credential.
The sign-in¶
from sesamcp.signin import LoginField, LoginOAuthProvider, LoginRejected, OidcLogin, build_store
async def check(form): # the form
if not await key_is_valid(form["api_key"]):
raise LoginRejected("This key is not valid.")
return {"api_key": form["api_key"]} # the grant: what headers_for gets
async def who(claims): # the provider's claims
if "pets" not in claims.get("groups", []):
raise LoginRejected("You may not use Pets.")
return None # allowed; the key comes from the link (asked for once)
provider = LoginOAuthProvider(
base_url="https://pets-mcp.example.org", service_name="Pets",
store=build_store("/data/oauth", secret=os.environ["MCP_OAUTH_SECRET"], salt="pets-mcp"),
fields=[LoginField("api_key", "API key", type="password", required=True)],
check_login=check,
oidc=OidcLogin(issuer="https://auth.example.org/application/o/pets/", client_id="...", name="Authentik"),
check_identity=who,
link=True,
)
mcp = build_server(..., auth=provider)
check_login(form) and check_identity(claims) return the grant or raise LoginRejected with the text the page shows. check_identity returning None asks for a link (with link=True). on_revoke(grant) runs when a client revokes; verify_static(token) accepts a raw token beside OAuth (MCP_AUTH_MODE=both).
Or let a config file describe it: sesamcp.login.build_provider(load_config(os.environ), client, store_dir).