Skip to content

Add OAuth authentication client for HTTPX #308

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,5 @@ cython_debug/
#.idea/

# vscode
.vscode/
.vscode/
.windsurfrules
3 changes: 1 addition & 2 deletions examples/clients/simple-chatbot/mcp_simple_chatbot/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,8 +322,7 @@ async def process_llm_response(self, llm_response: str) -> str:
total = result["total"]
percentage = (progress / total) * 100
logging.info(
f"Progress: {progress}/{total} "
f"({percentage:.1f}%)"
f"Progress: {progress}/{total} ({percentage:.1f}%)"
)

return f"Tool execution result: {result}"
Expand Down
3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,10 @@ mcp = "mcp.cli:app [cli]"
[tool.uv]
resolution = "lowest-direct"
dev-dependencies = [
"pyright>=1.1.391",
"pyright>=1.1.396",
"pytest>=8.3.4",
"ruff>=0.8.5",
"trio>=0.26.2",
"pytest-flakefinder>=1.1.0",
"pytest-xdist>=3.6.1",
]

Expand Down
15 changes: 13 additions & 2 deletions src/mcp/client/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
from urllib.parse import urlparse

import anyio
import httpx
from pydantic import AnyHttpUrl

from mcp.client.auth.httpx import McpOAuth
from mcp.client.auth.oauth import InMemoryOAuthStore, OAuthClient
from mcp.client.session import ClientSession
from mcp.client.sse import sse_client
from mcp.client.stdio import StdioServerParameters, stdio_client
Expand Down Expand Up @@ -46,8 +50,15 @@ async def main(command_or_url: str, args: list[str], env: list[tuple[str, str]])

if urlparse(command_or_url).scheme in ("http", "https"):
# Use SSE client for HTTP(S) URLs
async with sse_client(command_or_url) as streams:
await run_session(*streams)
oauth_client = OAuthClient(
client_name="mcp-client",
server_url=AnyHttpUrl(command_or_url),
redirect_url=AnyHttpUrl("http://localhost:5999/auth"),
provider=InMemoryOAuthStore(),
)
async with httpx.AsyncClient(auth=McpOAuth(oauth_client)) as http:
async with sse_client(http, command_or_url) as streams:
await run_session(*streams)
else:
# Use stdio client for commands
server_parameters = StdioServerParameters(
Expand Down
Empty file added src/mcp/client/auth/__init__.py
Empty file.
Loading