|
30 | 30 | # MCP client imports |
31 | 31 | try: |
32 | 32 | from mcp import ClientSession |
33 | | - from mcp.client.streamable_http import streamablehttp_client |
34 | | - import httpx |
| 33 | + from mcp_compose.http_client import streamable_http_client_compat |
35 | 34 | HAS_MCP = True |
36 | 35 | except ImportError: |
37 | 36 | HAS_MCP = False |
@@ -91,22 +90,18 @@ async def _list_tools(): |
91 | 90 | # Disable SSL verification for localhost (development with mkcert) |
92 | 91 | server_url = self.oauth.get_server_url() |
93 | 92 | verify_ssl = not server_url.startswith("https://localhost") |
94 | | - |
95 | | - # Create HTTP client with auth headers |
96 | | - async with httpx.AsyncClient( |
| 93 | + |
| 94 | + # Connect using non-deprecated streamable HTTP client |
| 95 | + async with streamable_http_client_compat( |
| 96 | + f"{self.oauth.get_server_url()}/mcp", |
97 | 97 | headers={"Authorization": f"Bearer {self.access_token}"}, |
98 | 98 | timeout=30.0, |
99 | | - verify=verify_ssl |
100 | | - ) as http_client: |
101 | | - # Connect using MCP SDK's streamable HTTP client |
102 | | - async with streamablehttp_client( |
103 | | - f"{self.oauth.get_server_url()}/mcp", |
104 | | - http_client=http_client |
105 | | - ) as (read_stream, write_stream): |
106 | | - async with ClientSession(read_stream, write_stream) as session: |
107 | | - await session.initialize() |
108 | | - tools_list = await session.list_tools() |
109 | | - return tools_list |
| 99 | + verify=verify_ssl, |
| 100 | + ) as (read_stream, write_stream, _): |
| 101 | + async with ClientSession(read_stream, write_stream) as session: |
| 102 | + await session.initialize() |
| 103 | + tools_list = await session.list_tools() |
| 104 | + return tools_list |
110 | 105 |
|
111 | 106 | # Run async function |
112 | 107 | tools_list = asyncio.run(_list_tools()) |
@@ -153,44 +148,40 @@ async def invoke_tool_mcp(self, tool_name: str, arguments: Dict[str, Any]) -> Op |
153 | 148 | # Disable SSL verification for localhost (development with mkcert) |
154 | 149 | server_url = self.oauth.get_server_url() |
155 | 150 | verify_ssl = not server_url.startswith("https://localhost") |
156 | | - |
157 | | - # Create HTTP client with auth headers |
158 | | - async with httpx.AsyncClient( |
| 151 | + |
| 152 | + # Connect using non-deprecated streamable HTTP client |
| 153 | + async with streamable_http_client_compat( |
| 154 | + f"{self.oauth.get_server_url()}/mcp", |
159 | 155 | headers={"Authorization": f"Bearer {self.access_token}"}, |
160 | 156 | timeout=30.0, |
161 | | - verify=verify_ssl |
162 | | - ) as http_client: |
163 | | - # Connect to MCP server via HTTP streaming |
164 | | - async with streamablehttp_client( |
165 | | - f"{self.oauth.get_server_url()}/mcp", |
166 | | - http_client=http_client |
167 | | - ) as (read_stream, write_stream): |
168 | | - async with ClientSession(read_stream, write_stream) as session: |
169 | | - # Initialize the session |
170 | | - await session.initialize() |
171 | | - |
172 | | - # Call the tool |
173 | | - result = await session.call_tool(tool_name, arguments) |
174 | | - |
175 | | - # Extract content from result |
176 | | - if hasattr(result, 'content'): |
177 | | - content = result.content |
178 | | - if isinstance(content, list) and len(content) > 0: |
179 | | - # Get the text content from the first item |
180 | | - first_content = content[0] |
181 | | - if hasattr(first_content, 'text'): |
182 | | - result_text = first_content.text |
183 | | - else: |
184 | | - result_text = str(first_content) |
| 157 | + verify=verify_ssl, |
| 158 | + ) as (read_stream, write_stream, _): |
| 159 | + async with ClientSession(read_stream, write_stream) as session: |
| 160 | + # Initialize the session |
| 161 | + await session.initialize() |
| 162 | + |
| 163 | + # Call the tool |
| 164 | + result = await session.call_tool(tool_name, arguments) |
| 165 | + |
| 166 | + # Extract content from result |
| 167 | + if hasattr(result, 'content'): |
| 168 | + content = result.content |
| 169 | + if isinstance(content, list) and len(content) > 0: |
| 170 | + # Get the text content from the first item |
| 171 | + first_content = content[0] |
| 172 | + if hasattr(first_content, 'text'): |
| 173 | + result_text = first_content.text |
185 | 174 | else: |
186 | | - result_text = str(content) |
| 175 | + result_text = str(first_content) |
187 | 176 | else: |
188 | | - result_text = str(result) |
189 | | - |
190 | | - print(f"✅ Tool invoked successfully") |
191 | | - print(f" Result: {result_text}") |
192 | | - |
193 | | - return result |
| 177 | + result_text = str(content) |
| 178 | + else: |
| 179 | + result_text = str(result) |
| 180 | + |
| 181 | + print(f"✅ Tool invoked successfully") |
| 182 | + print(f" Result: {result_text}") |
| 183 | + |
| 184 | + return result |
194 | 185 |
|
195 | 186 | except Exception as e: |
196 | 187 | print(f"❌ Error invoking tool: {e}") |
|
0 commit comments