19
19
from litellm .types .mcp import (
20
20
MCPAuth ,
21
21
MCPAuthType ,
22
- MCPSpecVersion ,
23
- MCPSpecVersionType ,
24
22
MCPStdioConfig ,
25
23
MCPTransport ,
26
24
MCPTransportType ,
@@ -48,7 +46,6 @@ def __init__(
48
46
auth_value : Optional [str ] = None ,
49
47
timeout : float = 60.0 ,
50
48
stdio_config : Optional [MCPStdioConfig ] = None ,
51
- protocol_version : MCPSpecVersionType = MCPSpecVersion .jun_2025 ,
52
49
):
53
50
self .server_url : str = server_url
54
51
self .transport_type : MCPTransport = transport_type
@@ -62,7 +59,6 @@ def __init__(
62
59
self ._session_ctx = None
63
60
self ._task : Optional [asyncio .Task ] = None
64
61
self .stdio_config : Optional [MCPStdioConfig ] = stdio_config
65
- self .protocol_version : MCPSpecVersionType = protocol_version
66
62
67
63
# handle the basic auth value if provided
68
64
if auth_value :
@@ -84,22 +80,24 @@ async def connect(self):
84
80
"""Initialize the transport and session."""
85
81
if self ._session :
86
82
return # Already connected
87
-
83
+
88
84
try :
89
85
if self .transport_type == MCPTransport .stdio :
90
86
# For stdio transport, use stdio_client with command-line parameters
91
87
if not self .stdio_config :
92
88
raise ValueError ("stdio_config is required for stdio transport" )
93
-
89
+
94
90
server_params = StdioServerParameters (
95
91
command = self .stdio_config .get ("command" , "" ),
96
92
args = self .stdio_config .get ("args" , []),
97
- env = self .stdio_config .get ("env" , {})
93
+ env = self .stdio_config .get ("env" , {}),
98
94
)
99
-
95
+
100
96
self ._transport_ctx = stdio_client (server_params )
101
97
self ._transport = await self ._transport_ctx .__aenter__ ()
102
- self ._session_ctx = ClientSession (self ._transport [0 ], self ._transport [1 ])
98
+ self ._session_ctx = ClientSession (
99
+ self ._transport [0 ], self ._transport [1 ]
100
+ )
103
101
self ._session = await self ._session_ctx .__aenter__ ()
104
102
await self ._session .initialize ()
105
103
elif self .transport_type == MCPTransport .sse :
@@ -110,7 +108,9 @@ async def connect(self):
110
108
headers = headers ,
111
109
)
112
110
self ._transport = await self ._transport_ctx .__aenter__ ()
113
- self ._session_ctx = ClientSession (self ._transport [0 ], self ._transport [1 ])
111
+ self ._session_ctx = ClientSession (
112
+ self ._transport [0 ], self ._transport [1 ]
113
+ )
114
114
self ._session = await self ._session_ctx .__aenter__ ()
115
115
await self ._session .initialize ()
116
116
else : # http
@@ -121,7 +121,9 @@ async def connect(self):
121
121
headers = headers ,
122
122
)
123
123
self ._transport = await self ._transport_ctx .__aenter__ ()
124
- self ._session_ctx = ClientSession (self ._transport [0 ], self ._transport [1 ])
124
+ self ._session_ctx = ClientSession (
125
+ self ._transport [0 ], self ._transport [1 ]
126
+ )
125
127
self ._session = await self ._session_ctx .__aenter__ ()
126
128
await self ._session .initialize ()
127
129
except ValueError as e :
@@ -185,7 +187,7 @@ def update_auth_value(self, mcp_auth_value: str):
185
187
def _get_auth_headers (self ) -> dict :
186
188
"""Generate authentication headers based on auth type."""
187
189
headers = {}
188
-
190
+
189
191
if self ._mcp_auth_value :
190
192
if self .auth_type == MCPAuth .bearer_token :
191
193
headers ["Authorization" ] = f"Bearer { self ._mcp_auth_value } "
@@ -196,18 +198,8 @@ def _get_auth_headers(self) -> dict:
196
198
elif self .auth_type == MCPAuth .authorization :
197
199
headers ["Authorization" ] = self ._mcp_auth_value
198
200
199
- # Handle protocol version - it might be a string or enum
200
- if hasattr (self .protocol_version , 'value' ):
201
- # It's an enum
202
- protocol_version_str = self .protocol_version .value
203
- else :
204
- # It's a string
205
- protocol_version_str = str (self .protocol_version )
206
-
207
- headers ["MCP-Protocol-Version" ] = protocol_version_str
208
201
return headers
209
202
210
-
211
203
async def list_tools (self ) -> List [MCPTool ]:
212
204
"""List available tools from the server."""
213
205
if not self ._session :
@@ -216,7 +208,7 @@ async def list_tools(self) -> List[MCPTool]:
216
208
except Exception as e :
217
209
verbose_logger .warning (f"MCP client connection failed: { str (e )} " )
218
210
return []
219
-
211
+
220
212
if self ._session is None :
221
213
verbose_logger .warning ("MCP client session is not initialized" )
222
214
return []
@@ -245,17 +237,20 @@ async def call_tool(
245
237
except Exception as e :
246
238
verbose_logger .warning (f"MCP client connection failed: { str (e )} " )
247
239
return MCPCallToolResult (
248
- content = [TextContent (type = "text" , text = f"{ str (e )} " )],
249
- isError = True
240
+ content = [TextContent (type = "text" , text = f"{ str (e )} " )], isError = True
250
241
)
251
242
252
243
if self ._session is None :
253
244
verbose_logger .warning ("MCP client session is not initialized" )
254
245
return MCPCallToolResult (
255
- content = [TextContent (type = "text" , text = "MCP client session is not initialized" )],
246
+ content = [
247
+ TextContent (
248
+ type = "text" , text = "MCP client session is not initialized"
249
+ )
250
+ ],
256
251
isError = True ,
257
252
)
258
-
253
+
259
254
try :
260
255
tool_result = await self ._session .call_tool (
261
256
name = call_tool_request_params .name ,
@@ -270,8 +265,8 @@ async def call_tool(
270
265
await self .disconnect ()
271
266
# Return a default error result instead of raising
272
267
return MCPCallToolResult (
273
- content = [TextContent (type = "text" , text = f"{ str (e )} " )], # Empty content for error case
268
+ content = [
269
+ TextContent (type = "text" , text = f"{ str (e )} " )
270
+ ], # Empty content for error case
274
271
isError = True ,
275
272
)
276
-
277
-
0 commit comments