MCPcopy Create free account
hub / github.com/Chainlit/cookbook / call_tool

Function call_tool

mcp/app.py:58–89  ·  view source on GitHub ↗
(tool_use)

Source from the content-addressed store, hash-verified

56
57@cl.step(type="tool")
58async def call_tool(tool_use):
59 tool_name = tool_use.name
60 tool_input = tool_use.input
61
62 current_step = cl.context.current_step
63 current_step.name = tool_name
64
65 # Identify which mcp is used
66 mcp_tools = cl.user_session.get("mcp_tools", {})
67 mcp_name = None
68
69 for connection_name, tools in mcp_tools.items():
70 if any(tool.get("name") == tool_name for tool in tools):
71 mcp_name = connection_name
72 break
73
74 if not mcp_name:
75 current_step.output = json.dumps({"error": f"Tool {tool_name} not found in any MCP connection"})
76 return current_step.output
77
78 mcp_session, _ = cl.context.session.mcp_sessions.get(mcp_name)
79
80 if not mcp_session:
81 current_step.output = json.dumps({"error": f"MCP {mcp_name} not found in any MCP connection"})
82 return current_step.output
83
84 try:
85 current_step.output = await mcp_session.call_tool(tool_name, tool_input)
86 except Exception as e:
87 current_step.output = json.dumps({"error": str(e)})
88
89 return current_step.output
90
91async def call_claude(chat_messages):
92 msg = cl.Message(content="")

Callers 1

on_messageFunction · 0.70

Calls 1

getMethod · 0.80

Tested by

no test coverage detected