Repository navigation
feat(agents): add AI agents and MCP support (CIMD and DCR) #215
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
KomanRudden
wants to merge
18
commits into
main
Choose a base branch
from
feat/agents-cimd-dcr
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+7,374
−2
Open
Changes from 13 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
f726dd0
feat(agents): add AI agents and MCP support (CIMD and DCR)
KomanRudden 6caf548
fix(agents): address review feedback
KomanRudden a532b62
docs(agents): add docstrings
KomanRudden bae6d8c
fix(agents): revoke only at the issuing server and let forget work of…
KomanRudden f6cf999
Updated .gitignore
KomanRudden a27211b
fix(agents): give each account its own DCR registration
KomanRudden a54fbc8
test(agents): cover async verification, revocation and cache limits
KomanRudden 72f0f99
fix(agents): delete stale registrations atomically and validate secre…
KomanRudden b3f37b3
fix(agents): address review findings in token refresh, discovery and …
KomanRudden 1377c0f
fix(agents): keep newer tokens on every failed refresh; take the Wind…
KomanRudden d3384d6
fix(agents): refuse internal network targets from public resources; k…
KomanRudden 66c1793
Merge branch 'main' into feat/agents-cimd-dcr
KomanRudden b02c048
Merge remote-tracking branch 'origin/main' into HEAD
KomanRudden 6694922
fix(agents): handle races, query resources and edge cases from review
KomanRudden e6c75ed
fix(agents): never resend one-shot data bodies, clarify revocation delay
KomanRudden d71cf9f
fix(agents): harden discovery, redirects, one-shot 401s and file storage
KomanRudden 997ec67
fix(agents): skip unusable metadata, keep the granted resource on ref…
KomanRudden 5f4ef03
fix(agents): skip metadata with an unusable authorization server URL
KomanRudden File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| """ | ||
| A CLI agent that signs a user in to a protected MCP server and calls it. | ||
|
|
||
| export RESOURCE_URL=https://<your public host>/mcp | ||
| # CIMD (preferred): the URL where client_metadata_host.py serves the document, | ||
| # whose host is in the Kinde application's Trusted agent hosts. Leave unset to use DCR. | ||
| export CLIENT_METADATA_URL=https://agent.onzs.co.za/oauth/client-metadata.json | ||
| python examples/agents/cli_agent.py | ||
|
|
||
| The browser opens for sign-in. Tokens are kept in ~/.kinde-example-agent/tokens.json. | ||
| """ | ||
|
|
||
| import asyncio | ||
| import os | ||
|
|
||
| from kinde_sdk.agents import FileAgentStorage, KindeAgentClient | ||
|
|
||
|
|
||
| async def main() -> None: | ||
| async with KindeAgentClient( | ||
| os.environ["RESOURCE_URL"], | ||
| client_metadata_url=os.getenv("CLIENT_METADATA_URL"), | ||
| client_name="Kinde example agent", | ||
| # A fixed port, so a DCR registration (tied to its exact redirect URI) is reused between runs | ||
| redirect_uri="http://127.0.0.1:8765/callback", | ||
| storage=FileAgentStorage("~/.kinde-example-agent/tokens.json"), | ||
| ) as client: | ||
| await client.connect() | ||
| print("Signed in:", await client.get_token_info()) | ||
|
|
||
| response = await client.request( | ||
| "POST", | ||
| json={ | ||
| "jsonrpc": "2.0", | ||
| "id": 1, | ||
| "method": "initialize", | ||
| "params": { | ||
| "protocolVersion": "2025-06-18", | ||
| "capabilities": {}, | ||
| "clientInfo": {"name": "kinde-example-agent", "version": "1"}, | ||
| }, | ||
| }, | ||
| headers={"Accept": "application/json, text/event-stream"}, | ||
| ) | ||
| print(response.status_code, response.text) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| asyncio.run(main()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| """ | ||
| Serves an agent's client ID metadata document (CIMD). | ||
|
|
||
| Kinde fetches this from the public internet, so run it on (or tunnel it to) the | ||
| public host in CLIENT_METADATA_URL, and add that host to the Kinde application's | ||
| Trusted agent hosts. | ||
|
|
||
| export CLIENT_METADATA_URL=https://agent.onzs.co.za/oauth/client-metadata.json | ||
| uvicorn examples.agents.client_metadata_host:app --port 8080 | ||
| """ | ||
|
|
||
| import os | ||
|
|
||
| from fastapi import FastAPI | ||
|
|
||
| from kinde_fastapi.agents import client_metadata_router | ||
| from kinde_sdk.agents import build_client_metadata_document | ||
|
|
||
| document = build_client_metadata_document( | ||
| os.environ["CLIENT_METADATA_URL"], | ||
| "Kinde example agent", | ||
| # Loopback works on any port for CIMD; cli_agent.py uses 8765 | ||
| ["http://127.0.0.1/callback"], | ||
| ) | ||
|
|
||
| app = FastAPI() | ||
| app.include_router(client_metadata_router(document)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| """ | ||
| A REST API protected by a Kinde "AI agents and MCP" application, with FastAPI. | ||
|
|
||
| pip install "kinde-python-sdk[fastapi]" | ||
| export KINDE_HOST=https://yourbiz.kinde.com | ||
| export KINDE_AGENTS_CLIENT_ID=... KINDE_AGENTS_CLIENT_SECRET=... | ||
| export KINDE_AGENTS_RESOURCE_URL=http://localhost:3001/api | ||
| uvicorn examples.agents.fastapi_resource_server:app --port 3001 | ||
|
|
||
| curl -i http://localhost:3001/api/docs # 401 with a WWW-Authenticate challenge | ||
| curl http://localhost:3001/.well-known/oauth-protected-resource/api | ||
| """ | ||
|
|
||
| from fastapi import Depends, FastAPI | ||
|
|
||
| from kinde_fastapi.agents import FastAPIAgentAuth | ||
| from kinde_sdk.agents import AgentAuthContext | ||
|
|
||
| app = FastAPI() | ||
| agents = FastAPIAgentAuth() | ||
| agents.install(app) | ||
|
|
||
|
|
||
| @app.get("/api/me") | ||
| async def me(auth: AgentAuthContext = Depends(agents.require())): | ||
| return {"user": auth.user_id, "agent": auth.client_id, "permissions": auth.permissions} | ||
|
|
||
|
|
||
| @app.get("/api/docs") | ||
| async def docs(auth: AgentAuthContext = Depends(agents.require_permission("read:docs"))): | ||
| return {"docs": "...", "user": auth.user_id} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| """ | ||
| An MCP server protected by a Kinde "AI agents and MCP" application. | ||
|
|
||
| pip install "kinde-python-sdk[mcp]" uvicorn | ||
| export KINDE_HOST=https://yourbiz.kinde.com | ||
| export KINDE_AGENTS_CLIENT_ID=... # the AI agents and MCP application | ||
| export KINDE_AGENTS_CLIENT_SECRET=... | ||
| export KINDE_AGENTS_RESOURCE_URL=https://<your public host>/mcp | ||
| python examples/agents/mcp_server.py | ||
|
|
||
| Connect with MCP Inspector (npx @modelcontextprotocol/inspector) or Claude Code | ||
| (claude mcp add --transport http kinde-test https://<your public host>/mcp). | ||
| """ | ||
|
|
||
| from urllib.parse import urlsplit | ||
|
|
||
| import uvicorn | ||
| from mcp.server.mcpserver import MCPServer | ||
| from mcp.server.mcpserver.exceptions import ToolError | ||
| from mcp.server.transport_security import TransportSecuritySettings | ||
|
|
||
| from kinde_sdk.agents import KindeAgentAuth, has_permission | ||
| from kinde_sdk.agents.mcp import ( | ||
| KindeTokenVerifier, | ||
| get_agent_auth_context, | ||
| kinde_auth_settings, | ||
| mount_authorization_server_metadata, | ||
| ) | ||
|
|
||
| auth = KindeAgentAuth() | ||
|
|
||
| server = MCPServer( | ||
| "Kinde example server", | ||
| token_verifier=KindeTokenVerifier(auth), | ||
| auth=kinde_auth_settings(auth), | ||
| ) | ||
| # Optional: some older MCP clients look for this on the server's own origin | ||
| mount_authorization_server_metadata(server, auth) | ||
|
|
||
|
|
||
| @server.tool() | ||
| async def whoami() -> str: | ||
| """Who is signed in, and through which agent.""" | ||
| context = get_agent_auth_context() | ||
| return f"User {context.user_id} via {context.client_id} with permissions {context.permissions}" | ||
|
|
||
|
|
||
| @server.tool() | ||
| async def read_docs() -> str: | ||
| """Needs the read:docs permission in Kinde.""" | ||
| if not has_permission(get_agent_auth_context(), "read:docs"): | ||
| # A tool error, not an HTTP 403: signing in again can't grant a permission | ||
| raise ToolError("You don't have permission to read docs.") | ||
| return "The docs." | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| # Accept the public host from the Resource URL (for example a tunnel) as well as local requests | ||
| public_host = urlsplit(auth.config.resource_url).netloc | ||
| security = TransportSecuritySettings(allowed_hosts=[public_host, "localhost:3001", "127.0.0.1:3001"]) | ||
| uvicorn.run(server.streamable_http_app(transport_security=security), host="127.0.0.1", port=3001) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.