Debian Codegraph @

examples/agents_comprehensive.py

raw
1 #!/usr/bin/env -S uv run python
2
3 import os
4 import time
5
6 from anthropic import Anthropic
7
8 MCP_SERVER_NAME = "github"
9 MCP_SERVER_URL = "https://api.githubcopilot.com/mcp/"
10
11 PROMPT = (
12 "Hi! List every tool and skill you have access to, grouped by where they "
13 "came from (built-in toolset, custom tool, MCP server, skills)."
14 )
15
16
17 def main() -> None:
18 anthropic = Anthropic(api_key=os.environ.get("ANTHROPIC_API_KEY"))
19
20 github_token = os.environ.get("GITHUB_TOKEN")
21 if not github_token:
22 raise RuntimeError("GITHUB_TOKEN is required (use a fine-grained PAT with public-repo read only)")
23
24 # Create an environment
25 environment = anthropic.beta.environments.create(
26 name="comprehensive-example-environment",
27 )
28 print("Created environment:", environment.id)
29
30 # Create a vault and store the MCP server credential in it
31 vault = anthropic.beta.vaults.create(display_name="comprehensive-example-vault")
32 print("Created vault:", vault.id)
33
34 credential = anthropic.beta.vaults.credentials.create(
35 vault.id,
36 display_name="github-mcp",
37 auth={
38 "type": "static_bearer",
39 "mcp_server_url": MCP_SERVER_URL,
40 "token": github_token,
41 },
42 )
43 print("Created credential:", credential.id)
44
45 # Upload a custom skill
46 skill_md_path = os.path.join(os.path.dirname(__file__), "greeting-SKILL.md")
47 with open(skill_md_path, "rb") as skill_file:
48 skill = anthropic.skills.create(
49 display_name=f"comprehensive-greeting-{int(time.time() * 1000)}",
50 files=[("greeting/SKILL.md", skill_file, "text/markdown")],
51 )
52 print("Created skill:", skill.id)
53
54 # Create v1 of the agent with the built-in toolset, an MCP server, and a custom tool
55 agent_v1 = anthropic.beta.agents.create(
56 name="comprehensive-example-agent",
57 model="claude-sonnet-5",
58 system="You are a helpful assistant.",
59 mcp_servers=[{"type": "url", "name": MCP_SERVER_NAME, "url": MCP_SERVER_URL}],
60 tools=[
61 {"type": "agent_toolset_20260401"},
62 {"type": "mcp_toolset", "mcp_server_name": MCP_SERVER_NAME},
63 {
64 "type": "custom",
65 "name": "get_weather",
66 "description": "Look up the current weather for a city.",
67 "input_schema": {
68 "type": "object",
69 "properties": {"city": {"type": "string"}},
70 "required": ["city"],
71 },
72 },
73 ],
74 )
75 print("Created agent v1:", agent_v1.id)
76
77 # Patch the agent to v2 by adding skills; each update bumps the version
78 agent = anthropic.beta.agents.update(
79 agent_v1.id,
80 version=agent_v1.version,
81 skills=[
82 {"type": "custom", "skill_id": skill.id},
83 {"type": "anthropic", "skill_id": "xlsx"},
84 ],
85 )
86 print("Patched agent to v2:", agent.id)
87
88 versions = anthropic.beta.agents.versions.list(agent.id)
89 print("Agent versions:", versions.data)
90
91 # Create a session pinned to v2; the vault supplies the MCP credential
92 session = anthropic.beta.sessions.create(
93 environment_id=environment.id,
94 agent={"type": "agent", "id": agent.id, "version": agent.version},
95 vault_ids=[vault.id],
96 )
97 print("Created session:", session.id)
98
99 # Send a prompt and stream events, answering the custom tool if called
100 print("Streaming events:")
101 anthropic.beta.sessions.events.send(
102 session.id,
103 events=[{"type": "user.message", "content": [{"type": "text", "text": PROMPT}]}],
104 )
105
106 with anthropic.beta.sessions.events.stream(session.id) as stream:
107 for event in stream:
108 print(event.to_json(indent=2))
109 # `get_weather` is a custom (non-builtin) tool, so the agent emits an
110 # `agent.custom_tool_use` event and expects a `user.custom_tool_result`.
111 if event.type == "agent.custom_tool_use" and event.name == "get_weather":
112 anthropic.beta.sessions.events.send(
113 session.id,
114 events=[
115 {
116 "type": "user.custom_tool_result",
117 "custom_tool_use_id": event.id,
118 "content": [{"type": "text", "text": '{"temperature_c": 14}'}],
119 }
120 ],
121 )
122 if event.type == "session.status_idle" and event.stop_reason and event.stop_reason.type == "end_turn":
123 break
124
125
126 if __name__ == "__main__":
127 main()