|
| 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( |
| 23 | + "GITHUB_TOKEN is required (use a fine-grained PAT with public-repo read only)" |
| 24 | + ) |
| 25 | + |
| 26 | + # Create an environment |
| 27 | + environment = anthropic.beta.environments.create( |
| 28 | + name="comprehensive-example-environment", |
| 29 | + ) |
| 30 | + print("Created environment:", environment.id) |
| 31 | + |
| 32 | + # Create a vault and store the MCP server credential in it |
| 33 | + vault = anthropic.beta.vaults.create(display_name="comprehensive-example-vault") |
| 34 | + print("Created vault:", vault.id) |
| 35 | + |
| 36 | + credential = anthropic.beta.vaults.credentials.create( |
| 37 | + vault.id, |
| 38 | + display_name="github-mcp", |
| 39 | + auth={ |
| 40 | + "type": "static_bearer", |
| 41 | + "mcp_server_url": MCP_SERVER_URL, |
| 42 | + "token": github_token, |
| 43 | + }, |
| 44 | + ) |
| 45 | + print("Created credential:", credential.id) |
| 46 | + |
| 47 | + # Upload a custom skill |
| 48 | + skill_md_path = os.path.join(os.path.dirname(__file__), "greeting-SKILL.md") |
| 49 | + with open(skill_md_path, "rb") as skill_file: |
| 50 | + skill = anthropic.beta.skills.create( |
| 51 | + display_title=f"comprehensive-greeting-{int(time.time() * 1000)}", |
| 52 | + files=[("greeting/SKILL.md", skill_file, "text/markdown")], |
| 53 | + ) |
| 54 | + print("Created skill:", skill.id) |
| 55 | + |
| 56 | + # Create v1 of the agent with the built-in toolset, an MCP server, and a custom tool |
| 57 | + agent_v1 = anthropic.beta.agents.create( |
| 58 | + name="comprehensive-example-agent", |
| 59 | + model="claude-sonnet-4-6", |
| 60 | + system="You are a helpful assistant.", |
| 61 | + mcp_servers=[{"type": "url", "name": MCP_SERVER_NAME, "url": MCP_SERVER_URL}], |
| 62 | + tools=[ |
| 63 | + {"type": "agent_toolset_20260401"}, |
| 64 | + {"type": "mcp_toolset", "mcp_server_name": MCP_SERVER_NAME}, |
| 65 | + { |
| 66 | + "type": "custom", |
| 67 | + "name": "get_weather", |
| 68 | + "description": "Look up the current weather for a city.", |
| 69 | + "input_schema": { |
| 70 | + "type": "object", |
| 71 | + "properties": {"city": {"type": "string"}}, |
| 72 | + "required": ["city"], |
| 73 | + }, |
| 74 | + }, |
| 75 | + ], |
| 76 | + ) |
| 77 | + print("Created agent v1:", agent_v1.id) |
| 78 | + |
| 79 | + # Patch the agent to v2 by adding skills; each update bumps the version |
| 80 | + agent = anthropic.beta.agents.update( |
| 81 | + agent_v1.id, |
| 82 | + version=agent_v1.version, |
| 83 | + skills=[ |
| 84 | + {"type": "custom", "skill_id": skill.id}, |
| 85 | + {"type": "anthropic", "skill_id": "xlsx"}, |
| 86 | + ], |
| 87 | + ) |
| 88 | + print("Patched agent to v2:", agent.id) |
| 89 | + |
| 90 | + versions = anthropic.beta.agents.versions.list(agent.id) |
| 91 | + print("Agent versions:", versions.data) |
| 92 | + |
| 93 | + # Create a session pinned to v2; the vault supplies the MCP credential |
| 94 | + session = anthropic.beta.sessions.create( |
| 95 | + environment_id=environment.id, |
| 96 | + agent={"type": "agent", "id": agent.id, "version": agent.version}, |
| 97 | + vault_ids=[vault.id], |
| 98 | + ) |
| 99 | + print("Created session:", session.id) |
| 100 | + |
| 101 | + # Send a prompt and stream events, answering the custom tool if called |
| 102 | + print("Streaming events:") |
| 103 | + anthropic.beta.sessions.events.send( |
| 104 | + session.id, |
| 105 | + events=[ |
| 106 | + {"type": "user.message", "content": [{"type": "text", "text": PROMPT}]} |
| 107 | + ], |
| 108 | + ) |
| 109 | + |
| 110 | + with anthropic.beta.sessions.events.stream(session.id) as stream: |
| 111 | + for event in stream: |
| 112 | + print(event.to_json(indent=2)) |
| 113 | + if event.type == "agent.custom_tool_use" and event.name == "get_weather": |
| 114 | + anthropic.beta.sessions.events.send( |
| 115 | + session.id, |
| 116 | + events=[ |
| 117 | + { |
| 118 | + "type": "user.custom_tool_result", |
| 119 | + "custom_tool_use_id": event.id, |
| 120 | + "content": [ |
| 121 | + {"type": "text", "text": '{"temperature_c": 14}'} |
| 122 | + ], |
| 123 | + } |
| 124 | + ], |
| 125 | + ) |
| 126 | + if ( |
| 127 | + event.type == "session.status_idle" |
| 128 | + and event.stop_reason |
| 129 | + and event.stop_reason.type == "end_turn" |
| 130 | + ): |
| 131 | + break |
| 132 | + |
| 133 | + |
| 134 | +if __name__ == "__main__": |
| 135 | + main() |
0 commit comments