AutoGen 워크벤치
AutoGen 워크벤치 (Workbench and MCP)
에이전트가 여러 도구를 써야 하는데, 도구들이 상태와 리소스를 공유해야 한다면 어떨까요? AutoGen의 Workbench는 바로 그런 경우를 위한 개념이에요. Workbench는 상태와 리소스를 공유하는 도구들의 모음입니다. 단일 도구에 대한 인터페이스를 제공하는 Tool과 달리, 워크벤치는 서로 다른 도구를 호출하고 같은 타입으로 결과를 받는 인터페이스를 제공해요.
Using Workbench
Workbench를 사용해 에이전트를 만드는 예시를 볼게요.
import json
from dataclasses import dataclass
from typing import List
from autogen_core import (
FunctionCall,
MessageContext,
RoutedAgent,
message_handler,
)
from autogen_core.model_context import ChatCompletionContext
from autogen_core.models import (
AssistantMessage,
ChatCompletionClient,
FunctionExecutionResultMessage,
SystemMessage,
UserMessage,
)
from autogen_core.tools import FunctionTool, Workbench
@dataclass
class Message:
content: str
class WorkbenchAgent(RoutedAgent):
def __init__(
self, model_client: ChatCompletionClient, model_context: ChatCompletionContext, workbench: Workbench
) -> None:
super().__init__("Workbench Agent")
self._model_client = model_client
self._model_context = model_context
self._workbench = workbench
@message_handler
async def handle_message(self, message: Message, ctx: MessageContext) -> None:
self._model_context.add_message(UserMessage(content=message.content, source="user"))
while True:
response = await self._model_client.create(
messages=self._model_context.get_messages(),
tools=self._workbench.get_tools(), # workbench의 도구를 모델에 전달한다.
cancellation_token=ctx.cancellation_token,
)
self._model_context.add_message(AssistantMessage(content=response.content, source="assistant"))
# 함수 호출을 실행한다.
for item in response.content:
if isinstance(item, FunctionCall):
function_call = item
arguments = json.loads(function_call.arguments)
result = await self._workbench.run_tool(
function_call.name, arguments, ctx.cancellation_token
)
self._model_context.add_message(
FunctionExecutionResultMessage(content=[result])
)
print(result)
else:
# 최종 답변일 때.
print(item)
return
이 예제에서 에이전트는 모델이 최종 답변을 반환할 때까지 워크벤치가 제공하는 도구를 루프로 호출해요.
MCP Workbench
Model Context Protocol (MCP)는 언어 모델에 도구와 리소스를 제공하기 위한 프로토콜이에요. MCP 서버는 도구 세트를 호스팅하고 그 상태를 관리하며, MCP 클라이언트는 언어 모델 쪽에서 동작해서 서버와 통신해 도구에 접근하고, 언어 모델이 도구를 효과적으로 쓰는 데 필요한 컨텍스트를 제공합니다.
AutoGen에서는 MCP 클라이언트를 구현하는 McpWorkbench를 제공해요. 이를 사용해서 MCP 서버가 제공하는 도구를 쓰는 에이전트를 만들 수 있습니다.
Web Browsing Agent using Playwright MCP
Playwright MCP 서버와 WorkbenchAgent 클래스를 써서 웹 브라우징 에이전트를 만드는 예시를 볼게요.
Playwright 브라우저 의존성을 설치해야 할 수도 있어요.
# npx playwright install chrome
터미널에서 Playwright MCP 서버를 시작합니다.
# npx @playwright/mcp@latest --port 8931
그런 다음 WorkbenchAgent 클래스와 McpWorkbench를 Playwright MCP 서버 URL과 함께 사용해 에이전트를 만듭니다.
from autogen_core import AgentId, SingleThreadedAgentRuntime
from autogen_core.model_context import BufferedChatCompletionContext
from autogen_ext.models.openai import OpenAIChatCompletionClient
from autogen_ext.tools.mcp import McpWorkbench, SseServerParams
# Playwright MCP 서버에 연결하는 MCP 워크벤치를 만든다.
mcp_workbench = await McpWorkbench.connect(
server_params=SseServerParams(
host="localhost",
port=8931,
)
)
# 모델과 워크벤치로 에이전트를 만든다.
model_client = OpenAIChatCompletionClient(model="gpt-4o")
agent = WorkbenchAgent(
model_client=model_client,
model_context=BufferedChatCompletionContext(buffer_size=20),
workbench=mcp_workbench, # type: ignore
)
await WorkbenchAgent.register(agent_type="WorkbenchAgent", runtime=runtime, factory=agent)
# 태스크를 보낸다.
runtime.start()
await runtime.send_message(
Message(content="Search for the weather in Seoul."),
AgentId("WorkbenchAgent", "default"),
)
await runtime.stop_when_idle()
이렇게 하면 에이전트가 Playwright MCP 서버가 제공하는 웹 브라우징 도구를 호출해서 웹 페이지를 검색·방문하는 동작을 수행할 수 있어요.