Skip to content

Commit d2f0ce4

Browse files
committed
examples, sensible defaults
1 parent 2060ea1 commit d2f0ce4

File tree

4 files changed

+58
-3
lines changed

4 files changed

+58
-3
lines changed
File renamed without changes.
File renamed without changes.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import asyncio
2+
3+
from mcp.types import TextContent
4+
5+
from fast_agent.agents.agent_types import AgentConfig
6+
from fast_agent.agents.tool_agent import ToolAgent
7+
from fast_agent.agents.tool_runner import ToolRunner
8+
from fast_agent.core import Core
9+
from fast_agent.llm.model_factory import ModelFactory
10+
from fast_agent.types import PromptMessageExtended
11+
12+
13+
def lookup_order_status(order_id: str) -> str:
14+
return f"Order {order_id} is packed and ready to ship."
15+
16+
17+
async def main() -> None:
18+
core: Core = Core()
19+
await core.initialize()
20+
21+
config = AgentConfig(name="order_bot")
22+
agent = ToolAgent(config, tools=[lookup_order_status], context=core.context)
23+
await agent.attach_llm(ModelFactory.create_factory("haiku"))
24+
25+
messages = [
26+
PromptMessageExtended(
27+
role="user",
28+
content=[
29+
TextContent(type="text", text="Check order 12345, then summarize in one line.")
30+
],
31+
)
32+
]
33+
34+
runner = ToolRunner(
35+
agent=agent,
36+
messages=messages,
37+
)
38+
39+
async for assistant_message in runner:
40+
text = assistant_message.last_text() or "<no text>"
41+
print(f"[assistant] {text}")
42+
43+
await core.cleanup()
44+
45+
46+
if __name__ == "__main__":
47+
asyncio.run(main())

src/fast_agent/agents/tool_runner.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
Union,
1010
)
1111

12-
from mcp.types import TextContent
12+
from mcp.types import ListToolsResult, TextContent
1313

1414
from fast_agent.constants import DEFAULT_MAX_ITERATIONS, FAST_AGENT_ERROR_CHANNEL
1515
from fast_agent.types import PromptMessageExtended, RequestParams
@@ -35,6 +35,8 @@ async def _tool_runner_llm_step(
3535

3636
async def run_tools(self, request: PromptMessageExtended) -> PromptMessageExtended: ...
3737

38+
async def list_tools(self) -> ListToolsResult: ...
39+
3840

3941
@dataclass(frozen=True)
4042
class ToolRunnerHooks:
@@ -67,8 +69,8 @@ def __init__(
6769
*,
6870
agent: _ToolLoopAgent,
6971
messages: list[PromptMessageExtended],
70-
request_params: RequestParams | None,
71-
tools: list[Tool] | None,
72+
request_params: RequestParams | None = None,
73+
tools: list[Tool] | None = None,
7274
hooks: ToolRunnerHooks | None = None,
7375
) -> None:
7476
self._agent = agent
@@ -95,6 +97,8 @@ async def __anext__(self) -> PromptMessageExtended:
9597
if self._done:
9698
raise StopAsyncIteration
9799

100+
await self._ensure_tools_ready()
101+
98102
if self._hooks.before_llm_call is not None:
99103
await self._hooks.before_llm_call(self, self._delta_messages)
100104

@@ -188,6 +192,10 @@ def _stage_tool_response(self, tool_message: PromptMessageExtended) -> None:
188192
self._delta_messages.append(self._last_message)
189193
self._delta_messages.append(tool_message)
190194

195+
async def _ensure_tools_ready(self) -> None:
196+
if self._tools is None:
197+
self._tools = (await self._agent.list_tools()).tools
198+
191199
async def _ensure_tool_response_staged(self) -> None:
192200
if self._pending_tool_request is None:
193201
return

0 commit comments

Comments
 (0)