Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
164 lines (138 loc) · 6.08 KB
/
Copy pathmain.py
File metadata and controls
164 lines (138 loc) · 6.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import asyncio
import os
import time
from mcp_agent.app import MCPApp
from mcp_agent.config import (
Settings,
LoggerSettings,
MCPSettings,
MCPServerSettings,
OpenAISettings,
AnthropicSettings,
)
from mcp_agent.agents.agent import Agent
from mcp_agent.workflows.llm.augmented_llm import RequestParams
from mcp_agent.workflows.llm.llm_selector import ModelPreferences
from mcp_agent.workflows.llm.augmented_llm_anthropic import AnthropicAugmentedLLM
from mcp_agent.workflows.llm.augmented_llm_openai import OpenAIAugmentedLLM
from mcp_agent.tracing.token_counter import TokenSummary
settings = Settings(
execution_engine="asyncio",
logger=LoggerSettings(type="file", level="debug"),
mcp=MCPSettings(
servers={
"fetch": MCPServerSettings(
command="uvx",
args=["mcp-server-fetch"],
),
"filesystem": MCPServerSettings(
command="npx",
args=["-y", "@modelcontextprotocol/server-filesystem"],
),
}
),
openai=OpenAISettings(
api_key="sk-my-openai-api-key",
default_model="gpt-4o-mini",
),
anthropic=AnthropicSettings(
api_key="sk-my-anthropic-api-key",
),
)
# Settings can either be specified programmatically,
# or loaded from mcp_agent.config.yaml/mcp_agent.secrets.yaml
app = MCPApp(name="mcp_basic_agent") # settings=settings)
@app.tool()
async def example_usage() -> str:
"""
An example function/tool that uses an agent with access to the fetch and filesystem
mcp servers. The agent will read the contents of mcp_agent.config.yaml, print the
first 2 paragraphs of the mcp homepage, and summarize the paragraphs into a tweet.
The example uses both OpenAI, Anthropic, and simulates a multi-turn conversation.
"""
async with app.run() as agent_app:
logger = agent_app.logger
context = agent_app.context
result = ""
logger.info("Current config:", data=context.config.model_dump())
# Add the current directory to the filesystem server's args
context.config.mcp.servers["filesystem"].args.extend([os.getcwd()])
finder_agent = Agent(
name="finder",
instruction="""You are an agent with access to the filesystem,
as well as the ability to fetch URLs. Your job is to identify
the closest match to a user's request, make the appropriate tool calls,
and return the URI and CONTENTS of the closest match.""",
server_names=["fetch", "filesystem"],
)
async with finder_agent:
logger.info("finder: Connected to server, calling list_tools...")
tools_list = await finder_agent.list_tools()
logger.info("Tools available:", data=tools_list.model_dump())
llm = await finder_agent.attach_llm(OpenAIAugmentedLLM)
result += await llm.generate_str(
message="Print the contents of mcp_agent.config.yaml verbatim",
)
logger.info(f"mcp_agent.config.yaml contents: {result}")
# Let's switch the same agent to a different LLM
llm = await finder_agent.attach_llm(AnthropicAugmentedLLM)
result += await llm.generate_str(
message="Print the first 2 paragraphs of https://modelcontextprotocol.io/introduction",
)
logger.info(f"First 2 paragraphs of Model Context Protocol docs: {result}")
result += "\n\n"
# Multi-turn conversations
result += await llm.generate_str(
message="Summarize those paragraphs in a 128 character tweet",
# You can configure advanced options by setting the request_params object
request_params=RequestParams(
# See https://modelcontextprotocol.io/docs/concepts/sampling#model-preferences for more details
modelPreferences=ModelPreferences(
costPriority=0.1, speedPriority=0.2, intelligencePriority=0.7
),
# You can also set the model directly using the 'model' field
# Generally request_params type aligns with the Sampling API type in MCP
),
)
logger.info(f"Paragraph as a tweet: {result}")
# Display final comprehensive token usage summary (use app convenience)
await display_token_summary(agent_app)
return result
async def display_token_summary(app_ctx: MCPApp, agent: Agent | None = None):
"""Display comprehensive token usage summary using app/agent convenience APIs."""
summary: TokenSummary = await app_ctx.get_token_summary()
print("\n" + "=" * 50)
print("TOKEN USAGE SUMMARY")
print("=" * 50)
# Total usage and cost
print("\nTotal Usage:")
print(f" Total tokens: {summary.usage.total_tokens:,}")
print(f" Input tokens: {summary.usage.input_tokens:,}")
print(f" Output tokens: {summary.usage.output_tokens:,}")
print(f" Total cost: ${summary.cost:.4f}")
# Breakdown by model
if summary.model_usage:
print("\nBreakdown by Model:")
for model_key, data in summary.model_usage.items():
print(f"\n {model_key}:")
print(
f" Tokens: {data.usage.total_tokens:,} (input: {data.usage.input_tokens:,}, output: {data.usage.output_tokens:,})"
)
print(f" Cost: ${data.cost:.4f}")
print("\n" + "=" * 50)
# Optional: show a specific agent's aggregated usage
if agent is not None:
agent_usage = await agent.get_token_usage()
if agent_usage:
print("\nAgent Usage:")
print(f" Agent: {agent.name}")
print(f" Total tokens: {agent_usage.total_tokens:,}")
print(f" Input tokens: {agent_usage.input_tokens:,}")
print(f" Output tokens: {agent_usage.output_tokens:,}")
print("\n" + "=" * 50)
if __name__ == "__main__":
start = time.time()
asyncio.run(example_usage())
end = time.time()
t = end - start
print(f"Total run time: {t:.2f}s")