Support different transports in Client (#1972)
This commit is contained in:
parent
2137c8fbdb
commit
21822053df
6 changed files with 544 additions and 631 deletions
|
|
@ -14,7 +14,7 @@ import threading
|
|||
import time
|
||||
import webbrowser
|
||||
from http.server import BaseHTTPRequestHandler, HTTPServer
|
||||
from typing import Any, Callable
|
||||
from typing import Any
|
||||
from urllib.parse import parse_qs, urlparse
|
||||
|
||||
import httpx
|
||||
|
|
@ -223,15 +223,15 @@ class SimpleAuthClient:
|
|||
auth=oauth_auth,
|
||||
timeout=60.0,
|
||||
) as (read_stream, write_stream):
|
||||
await self._run_session(read_stream, write_stream, None)
|
||||
await self._run_session(read_stream, write_stream)
|
||||
else:
|
||||
print("📡 Opening StreamableHTTP transport connection with auth...")
|
||||
async with httpx.AsyncClient(auth=oauth_auth, follow_redirects=True) as custom_client:
|
||||
async with streamable_http_client(
|
||||
url=self.server_url,
|
||||
http_client=custom_client,
|
||||
) as (read_stream, write_stream, get_session_id):
|
||||
await self._run_session(read_stream, write_stream, get_session_id)
|
||||
async with streamable_http_client(url=self.server_url, http_client=custom_client) as (
|
||||
read_stream,
|
||||
write_stream,
|
||||
):
|
||||
await self._run_session(read_stream, write_stream)
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ Failed to connect: {e}")
|
||||
|
|
@ -243,7 +243,6 @@ class SimpleAuthClient:
|
|||
self,
|
||||
read_stream: MemoryObjectReceiveStream[SessionMessage | Exception],
|
||||
write_stream: MemoryObjectSendStream[SessionMessage],
|
||||
get_session_id: Callable[[], str | None] | None = None,
|
||||
):
|
||||
"""Run the MCP session with the given streams."""
|
||||
print("🤝 Initializing MCP session...")
|
||||
|
|
@ -254,10 +253,6 @@ class SimpleAuthClient:
|
|||
print("✨ Session initialization complete!")
|
||||
|
||||
print(f"\n✅ Connected to MCP server at {self.server_url}")
|
||||
if get_session_id:
|
||||
session_id = get_session_id()
|
||||
if session_id:
|
||||
print(f"Session ID: {session_id}")
|
||||
|
||||
# Run interactive loop
|
||||
await self.interactive_loop()
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ from mcp.types import CallToolResult, TextContent
|
|||
|
||||
|
||||
async def run(url: str) -> None:
|
||||
async with streamable_http_client(url) as (read, write, _):
|
||||
async with streamable_http_client(url) as (read, write):
|
||||
async with ClientSession(read, write) as session:
|
||||
await session.initialize()
|
||||
|
||||
|
|
|
|||
|
|
@ -73,7 +73,7 @@ def get_text(result: CallToolResult) -> str:
|
|||
|
||||
|
||||
async def run(url: str) -> None:
|
||||
async with streamable_http_client(url) as (read, write, _):
|
||||
async with streamable_http_client(url) as (read, write):
|
||||
async with ClientSession(
|
||||
read,
|
||||
write,
|
||||
|
|
|
|||
|
|
@ -31,7 +31,7 @@ async def run_demo(url: str, items: int, checkpoint_every: int) -> None:
|
|||
print(f"Processing {items} items with checkpoints every {checkpoint_every}")
|
||||
print(f"{'=' * 60}\n")
|
||||
|
||||
async with streamable_http_client(url) as (read_stream, write_stream, _):
|
||||
async with streamable_http_client(url) as (read_stream, write_stream):
|
||||
async with ClientSession(read_stream, write_stream) as session:
|
||||
# Initialize the connection
|
||||
print("Initializing connection...")
|
||||
|
|
|
|||
|
|
@ -69,7 +69,7 @@ async def main():
|
|||
)
|
||||
|
||||
async with httpx.AsyncClient(auth=oauth_auth, follow_redirects=True) as custom_client:
|
||||
async with streamable_http_client("http://localhost:8001/mcp", http_client=custom_client) as (read, write, _):
|
||||
async with streamable_http_client("http://localhost:8001/mcp", http_client=custom_client) as (read, write):
|
||||
async with ClientSession(read, write) as session:
|
||||
await session.initialize()
|
||||
|
||||
|
|
|
|||
|
|
@ -10,11 +10,7 @@ from mcp.client.streamable_http import streamable_http_client
|
|||
|
||||
async def main():
|
||||
# Connect to a streamable HTTP server
|
||||
async with streamable_http_client("http://localhost:8000/mcp") as (
|
||||
read_stream,
|
||||
write_stream,
|
||||
_,
|
||||
):
|
||||
async with streamable_http_client("http://localhost:8000/mcp") as (read_stream, write_stream):
|
||||
# Create a session using the client streams
|
||||
async with ClientSession(read_stream, write_stream) as session:
|
||||
# Initialize the connection
|
||||
|
|
|
|||
Loading…
Reference in a new issue