88
99from mcp import ClientSession , StdioServerParameters , stdio_client
1010from mcp import Tool as MCPTool
11- from mcp .types import CallToolResult , ImageContent , TextContent
11+ from mcp .types import CallToolResult , ContentBlock , TextContent
1212
1313from agentlab .actions import FunctionSpec , ToolCall , ToolSpec
1414from agentlab .backends .browser .base import BrowserBackend
@@ -24,7 +24,7 @@ def __init__(self, config_path: str, read_timeout_seconds: int = 10) -> None:
2424 self .tool_to_server : dict [str , str ] = {}
2525 self .read_timeout_seconds = read_timeout_seconds
2626 self .exit_stack = AsyncExitStack ()
27- self .loop = None
27+ self .loop : asyncio . AbstractEventLoop
2828
2929 def initialize (self ):
3030 try :
@@ -125,15 +125,15 @@ def check_tool_exists(self, tool_name):
125125 raise Exception (f"Tool { tool_name } not found in any of the MCP servers" )
126126 return server_name
127127
128- def actions (self ) -> tuple [ToolSpec ]:
129- return (
128+ def actions (self ) -> list [ToolSpec ]:
129+ return [
130130 ToolSpec (
131131 function = FunctionSpec (
132132 name = tool .name , description = tool .description or "" , parameters = tool .inputSchema
133133 )
134134 )
135135 for tool in self .tools .values ()
136- )
136+ ]
137137
138138 async def aclose (self ) -> None :
139139 await self .exit_stack .aclose ()
@@ -144,28 +144,28 @@ def close(self) -> None:
144144
145145class MCPBrowserBackend (BrowserBackend ):
146146 config_path : str
147- _mcp = None
147+ _mcp : MCPClient
148148
149149 def initialize (self ) -> None :
150150 self ._mcp = MCPClient (config_path = self .config_path )
151151 self ._mcp .initialize ()
152152
153153 def step (self , action : ToolCall ) -> dict :
154154 contents = self .call_tool (action .name , action .arguments )
155- text = "\n " .join ([c .text for c in contents if c .type == "text" ])
155+ action_result = "\n " .join ([c .text for c in contents if c .type == "text" ])
156156 images = [c for c in contents if c .type == "image" ]
157157 return {
158- "text " : text ,
158+ "action_result " : action_result ,
159159 "screenshot" : images [- 1 ] if images else None ,
160160 }
161161
162- def call_tool (self , tool_name : str , arguments : dict ) -> list [TextContent | ImageContent ]:
162+ def call_tool (self , tool_name : str , arguments : dict ) -> list [ContentBlock ]:
163163 tool_result = self ._mcp .call_tool (tool_name , arguments )
164164 if tool_result .isError :
165- return [TextContent (text = f"Error calling tool { tool_name } " )] + tool_result .content
165+ return [TextContent (type = "text" , text = f"Error calling tool { tool_name } " )] + tool_result .content
166166 return tool_result .content
167167
168- def actions (self ) -> tuple [ToolSpec ]:
168+ def actions (self ) -> list [ToolSpec ]:
169169 return list (self ._mcp .actions ())
170170
171171 def close (self ) -> None :
0 commit comments