File: mcp_apikey.py

package info (click to toggle)
python-azure 20251118%2Bgit-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 783,356 kB
  • sloc: python: 6,474,533; ansic: 804; javascript: 287; sh: 205; makefile: 198; xml: 109
file content (42 lines) | stat: -rw-r--r-- 1,284 bytes parent folder | download
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
# Copyright (c) Microsoft. All rights reserved.

import asyncio
import os

from agent_framework import MCPStreamableHTTPTool
from agent_framework.azure import AzureOpenAIChatClient
from azure.identity import DefaultAzureCredential
from dotenv import load_dotenv

from azure.ai.agentserver.agentframework import from_agent_framework

MCP_TOOL_NAME = "github"  # Expected tool name exposed by the GitHub MCP server
MCP_TOOL_URL = "https://api.githubcopilot.com/mcp/"  # Base MCP server endpoint

load_dotenv()


async def main() -> None:
    github_token = os.getenv("GITHUB_TOKEN")
    if not github_token:
        raise RuntimeError(
            "GITHUB_TOKEN environment variable not set. Provide a GitHub token with MCP access."
        )

    agent = AzureOpenAIChatClient(credential=DefaultAzureCredential()).create_agent(
        instructions="You are a helpful assistant that answers GitHub questions. Use only the exposed MCP tools.",
        tools=MCPStreamableHTTPTool(
            name=MCP_TOOL_NAME,
            url=MCP_TOOL_URL,
            headers={
                "Authorization": f"Bearer {github_token}",
            },
        ),
    )

    async with agent:
        await from_agent_framework(agent).run_async()


if __name__ == "__main__":
    asyncio.run(main())