Skip to content

Commit cbd8745

Browse files
authored
Add guide for workbench and mcp & bug fixes for create_mcp_server_session (#6392)
Add user guide for workbench and mcp *Also fixed a bug in create_mcp_server_session that included "type" in the parameters Resolves: #6392
1 parent f059262 commit cbd8745

5 files changed

Lines changed: 355 additions & 1 deletion

File tree

python/packages/autogen-core/docs/src/user-guide/core-user-guide/components/workbench.ipynb

Lines changed: 327 additions & 0 deletions
Large diffs are not rendered by default.

python/packages/autogen-core/docs/src/user-guide/core-user-guide/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ framework/component-config
4848
components/model-clients
4949
components/model-context
5050
components/tools
51+
components/workbench
5152
components/command-line-code-executors
5253
```
5354

python/packages/autogen-core/src/autogen_core/tools/_workbench.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,28 @@ class ToolResult(BaseModel):
5252
is_error: bool = False
5353
"""Whether the tool execution resulted in an error."""
5454

55+
def to_text(self, replace_image: str | None = None) -> str:
56+
"""
57+
Convert the result to a text string.
58+
59+
Args:
60+
replace_image (str | None): The string to replace the image content with.
61+
If None, the image content will be included in the text as base64 string.
62+
63+
Returns:
64+
str: The text representation of the result.
65+
"""
66+
parts: List[str] = []
67+
for content in self.result:
68+
if isinstance(content, TextResultContent):
69+
parts.append(content.content)
70+
elif isinstance(content, ImageResultContent):
71+
if replace_image is not None:
72+
parts.append(replace_image)
73+
else:
74+
parts.append(f"[Image: {content.content.to_base64()}]")
75+
return "\n".join(parts)
76+
5577

5678
class Workbench(ABC, ComponentBase[BaseModel]):
5779
"""

python/packages/autogen-core/tests/test_workbench.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,15 @@ def test_tool_func_2(x: Annotated[int, "The number to add 2."]) -> int:
5757
assert result_1.name == "test_tool_1"
5858
assert result_1.result[0].type == "TextResultContent"
5959
assert result_1.result[0].content == "10"
60+
assert result_1.to_text() == "10"
6061
assert result_1.is_error is False
6162

6263
# Call tool with error
6364
result_2 = await workbench.call_tool("test_tool_2", {"x": 5})
6465
assert result_2.name == "test_tool_2"
6566
assert result_2.result[0].type == "TextResultContent"
6667
assert result_2.result[0].content == "This is a test error"
68+
assert result_2.to_text() == "This is a test error"
6769
assert result_2.is_error is True
6870

6971
# Save state.
@@ -109,11 +111,13 @@ def test_tool_func_2(x: Annotated[int, "The number to add 2."]) -> int:
109111
assert result_1.name == "test_tool_1"
110112
assert result_1.result[0].type == "TextResultContent"
111113
assert result_1.result[0].content == "10"
114+
assert result_1.to_text() == "10"
112115
assert result_1.is_error is False
113116

114117
# Call tool with error
115118
result_2 = await new_workbench.call_tool("test_tool_2", {"x": 5})
116119
assert result_2.name == "test_tool_2"
117120
assert result_2.result[0].type == "TextResultContent"
118121
assert result_2.result[0].content == "This is a test error"
122+
assert result_2.to_text() == "This is a test error"
119123
assert result_2.is_error is True

python/packages/autogen-ext/src/autogen_ext/tools/mcp/_session.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ async def create_mcp_server_session(
2323
) as session:
2424
yield session
2525
elif isinstance(server_params, SseServerParams):
26-
async with sse_client(**server_params.model_dump()) as (read, write):
26+
async with sse_client(**server_params.model_dump(exclude={"type"})) as (read, write):
2727
async with ClientSession(read_stream=read, write_stream=write) as session:
2828
yield session

0 commit comments

Comments
 (0)