4646# or loaded from mcp_agent.config.yaml/mcp_agent.secrets.yaml
4747app = MCPApp (name = "mcp_basic_agent" ) # settings=settings)
4848
49-
50- async def example_usage ():
49+ @app .tool ()
50+ async def example_usage ()-> str :
51+ """
52+ An example function/tool that uses an agent with access to the fetch and filesystem
53+ mcp servers. The agent will read the contents of mcp_agent.config.yaml, print the
54+ first 2 paragraphs of the mcp homepage, and summarize the paragraphs into a tweet.
55+ The example uses both OpenAI, Anthropic, and simulates a multi-turn conversation.
56+ """
5157 async with app .run () as agent_app :
5258 logger = agent_app .logger
5359 context = agent_app .context
60+ result = ""
5461
5562 logger .info ("Current config:" , data = context .config .model_dump ())
5663
@@ -68,25 +75,26 @@ async def example_usage():
6875
6976 async with finder_agent :
7077 logger .info ("finder: Connected to server, calling list_tools..." )
71- result = await finder_agent .list_tools ()
72- logger .info ("Tools available:" , data = result .model_dump ())
78+ tools_list = await finder_agent .list_tools ()
79+ logger .info ("Tools available:" , data = tools_list .model_dump ())
7380
7481 llm = await finder_agent .attach_llm (OpenAIAugmentedLLM )
75- result = await llm .generate_str (
82+ result + = await llm .generate_str (
7683 message = "Print the contents of mcp_agent.config.yaml verbatim" ,
7784 )
7885 logger .info (f"mcp_agent.config.yaml contents: { result } " )
7986
8087 # Let's switch the same agent to a different LLM
8188 llm = await finder_agent .attach_llm (AnthropicAugmentedLLM )
8289
83- result = await llm .generate_str (
90+ result + = await llm .generate_str (
8491 message = "Print the first 2 paragraphs of https://modelcontextprotocol.io/introduction" ,
8592 )
8693 logger .info (f"First 2 paragraphs of Model Context Protocol docs: { result } " )
94+ result += "\n \n "
8795
8896 # Multi-turn conversations
89- result = await llm .generate_str (
97+ result + = await llm .generate_str (
9098 message = "Summarize those paragraphs in a 128 character tweet" ,
9199 # You can configure advanced options by setting the request_params object
92100 request_params = RequestParams (
@@ -101,8 +109,9 @@ async def example_usage():
101109 logger .info (f"Paragraph as a tweet: { result } " )
102110
103111 # Display final comprehensive token usage summary (use app convenience)
104- await display_token_summary (agent_app , finder_agent )
112+ await display_token_summary (agent_app )
105113
114+ return result
106115
107116async def display_token_summary (app_ctx : MCPApp , agent : Agent | None = None ):
108117 """Display comprehensive token usage summary using app/agent convenience APIs."""
@@ -129,6 +138,8 @@ async def display_token_summary(app_ctx: MCPApp, agent: Agent | None = None):
129138 )
130139 print (f" Cost: ${ data .cost :.4f} " )
131140
141+ print ("\n " + "=" * 50 )
142+
132143 # Optional: show a specific agent's aggregated usage
133144 if agent is not None :
134145 agent_usage = await agent .get_token_usage ()
0 commit comments