|
| 1 | +import json |
| 2 | +import logging |
| 3 | +from urllib.parse import urlparse |
| 4 | + |
| 5 | +import fire |
| 6 | +import httpx |
| 7 | +from llama_stack_client import Agent, AgentEventLogger, LlamaStackClient |
| 8 | +from llama_stack_client.lib import get_oauth_token_for_mcp_server |
| 9 | +from rich import print as rprint |
| 10 | + |
| 11 | +logging.basicConfig(level=logging.INFO) |
| 12 | +logger = logging.getLogger(__name__) |
| 13 | + |
| 14 | + |
| 15 | +import tempfile |
| 16 | +from pathlib import Path |
| 17 | + |
| 18 | +TMP_DIR = Path(tempfile.gettempdir()) / "llama-stack" |
| 19 | +TMP_DIR.mkdir(parents=True, exist_ok=True) |
| 20 | + |
| 21 | +CACHE_FILE = TMP_DIR / "mcp_tokens.json" |
| 22 | + |
| 23 | + |
| 24 | +def main(model_id: str, mcp_servers: str = "https://mcp.asana.com/sse", llama_stack_url: str = "http://localhost:8321"): |
| 25 | + """Run an MCP agent with the specified model and servers. |
| 26 | +
|
| 27 | + Args: |
| 28 | + model_id: The model to use for the agent. |
| 29 | + mcp_servers: Comma-separated list of MCP servers to use for the agent. |
| 30 | + llama_stack_url: The URL of the Llama Stack server to use. |
| 31 | +
|
| 32 | + Examples: |
| 33 | + python mcp_agent.py "meta-llama/Llama-4-Scout-17B-16E-Instruct" \ |
| 34 | + -m "https://mcp.asana.com/sse" \ |
| 35 | + -l "http://localhost:8321" |
| 36 | + """ |
| 37 | + client = LlamaStackClient(base_url=llama_stack_url) |
| 38 | + if not check_model_exists(client, model_id): |
| 39 | + return |
| 40 | + |
| 41 | + servers = [s.strip() for s in mcp_servers.split(",")] |
| 42 | + mcp_headers = get_and_cache_mcp_headers(servers) |
| 43 | + |
| 44 | + toolgroup_ids = [] |
| 45 | + for server in servers: |
| 46 | + # we cannot use "/" in the toolgroup_id because we have some tech debt from earlier which uses |
| 47 | + # "/" as a separator for toolgroup_id and tool_name. We should fix this in the future. |
| 48 | + group_id = urlparse(server).netloc |
| 49 | + toolgroup_ids.append(group_id) |
| 50 | + client.toolgroups.register( |
| 51 | + toolgroup_id=group_id, mcp_endpoint=dict(uri=server), provider_id="model-context-protocol" |
| 52 | + ) |
| 53 | + |
| 54 | + agent = Agent( |
| 55 | + client=client, |
| 56 | + model=model_id, |
| 57 | + instructions="You are a helpful assistant who can use tools when necessary to answer questions.", |
| 58 | + tools=toolgroup_ids, |
| 59 | + extra_headers={ |
| 60 | + "X-LlamaStack-Provider-Data": json.dumps( |
| 61 | + { |
| 62 | + "mcp_headers": mcp_headers, |
| 63 | + } |
| 64 | + ), |
| 65 | + }, |
| 66 | + ) |
| 67 | + |
| 68 | + session_id = agent.create_session("test-session") |
| 69 | + |
| 70 | + while True: |
| 71 | + user_input = input("Enter a question: ") |
| 72 | + if user_input.lower() in ("q", "quit", "exit", "bye", ""): |
| 73 | + print("Exiting...") |
| 74 | + break |
| 75 | + response = agent.create_turn( |
| 76 | + session_id=session_id, |
| 77 | + messages=[{"role": "user", "content": user_input}], |
| 78 | + stream=True, |
| 79 | + ) |
| 80 | + for log in AgentEventLogger().log(response): |
| 81 | + log.print() |
| 82 | + |
| 83 | + |
| 84 | +def check_model_exists(client: LlamaStackClient, model_id: str) -> bool: |
| 85 | + models = [m for m in client.models.list() if m.model_type == "llm"] |
| 86 | + if model_id not in [m.identifier for m in models]: |
| 87 | + rprint(f"[red]Model {model_id} not found[/red]") |
| 88 | + rprint("[yellow]Available models:[/yellow]") |
| 89 | + for model in models: |
| 90 | + rprint(f" - {model.identifier}") |
| 91 | + return False |
| 92 | + return True |
| 93 | + |
| 94 | + |
| 95 | +def get_and_cache_mcp_headers(servers: list[str]) -> dict[str, dict[str, str]]: |
| 96 | + mcp_headers = {} |
| 97 | + |
| 98 | + logger.info(f"Using cache file: {CACHE_FILE} for MCP tokens") |
| 99 | + tokens = {} |
| 100 | + if CACHE_FILE.exists(): |
| 101 | + with open(CACHE_FILE, "r") as f: |
| 102 | + tokens = json.load(f) |
| 103 | + for server, token in tokens.items(): |
| 104 | + mcp_headers[server] = { |
| 105 | + "Authorization": f"Bearer {token}", |
| 106 | + } |
| 107 | + |
| 108 | + for server in servers: |
| 109 | + with httpx.Client() as http_client: |
| 110 | + headers = mcp_headers.get(server, {}) |
| 111 | + try: |
| 112 | + response = http_client.get(server, headers=headers, timeout=1.0) |
| 113 | + except httpx.TimeoutException: |
| 114 | + # timeout means success since we did not get an immediate 40X |
| 115 | + continue |
| 116 | + |
| 117 | + if response.status_code in (401, 403): |
| 118 | + logger.info(f"Server {server} requires authentication, getting token") |
| 119 | + token = get_oauth_token_for_mcp_server(server) |
| 120 | + if not token: |
| 121 | + logger.error(f"No token obtained for {server}") |
| 122 | + return |
| 123 | + |
| 124 | + tokens[server] = token |
| 125 | + mcp_headers[server] = { |
| 126 | + "Authorization": f"Bearer {token}", |
| 127 | + } |
| 128 | + |
| 129 | + with open(CACHE_FILE, "w") as f: |
| 130 | + json.dump(tokens, f, indent=2) |
| 131 | + |
| 132 | + return mcp_headers |
| 133 | + |
| 134 | + |
| 135 | +if __name__ == "__main__": |
| 136 | + fire.Fire(main) |
0 commit comments