Skip to content

Commit ec7b0db

Browse files
committed
Add semantic kernel with MCP HTTP example
1 parent c822cdc commit ec7b0db

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import asyncio
2+
import os
3+
4+
import azure.identity
5+
from dotenv import load_dotenv
6+
from openai import AsyncAzureOpenAI, AsyncOpenAI
7+
from semantic_kernel.agents import ChatCompletionAgent
8+
from semantic_kernel.connectors.ai.open_ai import OpenAIChatCompletion
9+
from semantic_kernel.connectors.mcp import MCPStreamableHttpPlugin
10+
11+
load_dotenv(override=True)
12+
API_HOST = os.getenv("API_HOST", "github")
13+
if API_HOST == "azure":
14+
token_provider = azure.identity.get_bearer_token_provider(azure.identity.DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default")
15+
chat_client = AsyncAzureOpenAI(
16+
api_version=os.environ["AZURE_OPENAI_VERSION"],
17+
azure_endpoint=os.environ["AZURE_OPENAI_ENDPOINT"],
18+
azure_ad_token_provider=token_provider,
19+
)
20+
chat_completion_service = OpenAIChatCompletion(ai_model_id=os.environ["AZURE_OPENAI_CHAT_MODEL"], async_client=chat_client)
21+
else:
22+
chat_client = AsyncOpenAI(api_key=os.environ["GITHUB_TOKEN"], base_url="https://models.inference.ai.azure.com")
23+
chat_completion_service = OpenAIChatCompletion(ai_model_id=os.getenv("GITHUB_MODEL", "gpt-4o"), async_client=chat_client)
24+
25+
26+
async def main():
27+
async with MCPStreamableHttpPlugin(
28+
name="TravelServer",
29+
description="Search hotels and flights",
30+
url="http://localhost:8000/mcp",
31+
) as mcp_http_plugin:
32+
agent = ChatCompletionAgent(name="travel_agent", instructions="You are a travel planning agent. You can help users find hotels.", service=chat_completion_service, plugins=[mcp_http_plugin])
33+
response = await agent.get_response(messages="Find me a hotel in San Francisco for 2 nights starting from 2024-01-01. I need a hotel with free WiFi and a pool.")
34+
print(response.content)
35+
36+
37+
if __name__ == "__main__":
38+
asyncio.run(main())

0 commit comments

Comments
 (0)