# Copyright (c) Microsoft. All rights reserved.

"""
GitHub Copilot Agent with MCP Servers

This sample demonstrates how to configure MCP (Model Context Protocol) servers
with GitHubCopilotAgent. It shows both local (stdio) and remote (HTTP) server
configurations, giving the agent access to external tools and data sources.

SECURITY NOTE: MCP servers can expose powerful capabilities. Only configure
servers you trust. The permission handler below prompts the user for approval
of MCP-related actions.
"""

import asyncio

from agent_framework.github import GitHubCopilotAgent
from copilot.generated.session_events import PermissionRequest
from copilot.session import MCPServerConfig, PermissionRequestResult
from dotenv import load_dotenv

# Load environment variables from .env file
load_dotenv()


def prompt_permission(request: PermissionRequest, context: dict[str, str]) -> PermissionRequestResult:
    """Permission handler that prompts the user for approval."""
    print(f"\n[Permission Request: {request.kind}]")

    response = input("Approve? (y/n): ").strip().lower()
    if response in ("y", "yes"):
        return PermissionRequestResult(kind="approved")
    return PermissionRequestResult(kind="denied-interactively-by-user")


async def main() -> None:
    print("=== GitHub Copilot Agent with MCP Servers ===\n")

    # Configure both local and remote MCP servers
    mcp_servers: dict[str, MCPServerConfig] = {
        # Local stdio server: provides filesystem access tools
        "filesystem": {
            "type": "stdio",
            "command": "npx",
            "args": ["-y", "@modelcontextprotocol/server-filesystem", "."],
            "tools": ["*"],
        },
        # Remote HTTP server: Microsoft Learn documentation
        "microsoft-learn": {
            "type": "http",
            "url": "https://learn.microsoft.com/api/mcp",
            "tools": ["*"],
        },
    }

    agent = GitHubCopilotAgent(
        instructions="You are a helpful assistant with access to the local filesystem and Microsoft Learn.",
        default_options={
            "on_permission_request": prompt_permission,
            "mcp_servers": mcp_servers,
        },
    )

    async with agent:
        # Query that exercises the local filesystem MCP server
        query1 = "List the files in the current directory"
        print(f"User: {query1}")
        result1 = await agent.run(query1)
        print(f"Agent: {result1}\n")

        # Query that exercises the remote Microsoft Learn MCP server
        # Remote MCP calls may take longer, so increase the timeout
        query2 = "Search Microsoft Learn for 'Azure Functions Python' and summarize the top result"
        print(f"User: {query2}")
        result2 = await agent.run(query2, options={"timeout": 120})
        print(f"Agent: {result2}\n")


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