Skip to content

Commit 3d142a8

Browse files
committed
feat: add error handling for MCP server connection
1 parent f1bbb3a commit 3d142a8

File tree

1 file changed

+24
-21
lines changed
  • src/datapilot/core/mcp_utils

1 file changed

+24
-21
lines changed

src/datapilot/core/mcp_utils/mcp.py

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -135,25 +135,28 @@ async def list_tools(command: str, args: list[str], env: dict[str, str]):
135135
if not command_path:
136136
raise click.UsageError(f"Command not found: {command}")
137137

138-
server_params = StdioServerParameters(
139-
command=command_path,
140-
args=args,
141-
env=env, # Now using processed env
142-
)
143-
144-
async with stdio_client(server_params) as (read, write):
145-
async with ClientSession(read, write) as session:
146-
await session.initialize()
147-
tools = await session.list_tools()
148-
mcp_tools = [
149-
{
150-
"name": tool.name,
151-
"description": tool.description,
152-
"inputSchema": tool.inputSchema,
153-
}
154-
for tool in tools.tools
155-
]
138+
try:
139+
server_params = StdioServerParameters(
140+
command=command_path,
141+
args=args,
142+
env=env, # Now using processed env
143+
)
156144

157-
return {
158-
"tools": mcp_tools,
159-
}
145+
async with stdio_client(server_params) as (read, write):
146+
async with ClientSession(read, write) as session:
147+
await session.initialize()
148+
tools = await session.list_tools()
149+
mcp_tools = [
150+
{
151+
"name": tool.name,
152+
"description": tool.description,
153+
"inputSchema": tool.inputSchema,
154+
}
155+
for tool in tools.tools
156+
]
157+
158+
return {
159+
"tools": mcp_tools,
160+
}
161+
except Exception as e:
162+
raise click.UsageError("Could not connect to MCP server: " + str(e))

0 commit comments

Comments
 (0)