Skip to content

Commit 6a1b0a5

Browse files
authored
Merge pull request #5 from madebygps/main
small refactor on framework examples
2 parents 11a7d7a + 789292f commit 6a1b0a5

File tree

2 files changed

+61
-24
lines changed

2 files changed

+61
-24
lines changed

agentframework_mcp_learn.py

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,31 @@
44
import logging
55
import os
66

7-
7+
from azure.identity import DefaultAzureCredential
88
from dotenv import load_dotenv
99
from rich import print
1010
from rich.logging import RichHandler
11+
1112
from agent_framework import ChatAgent, MCPStreamableHTTPTool
1213
from agent_framework.azure import AzureOpenAIChatClient
1314
from agent_framework.openai import OpenAIChatClient
14-
from azure.identity import DefaultAzureCredential
1515

16-
logging.basicConfig(level=logging.WARNING, format="%(message)s", datefmt="[%X]", handlers=[RichHandler()])
16+
# Configure logging
17+
logging.basicConfig(
18+
level=logging.WARNING,
19+
format="%(message)s",
20+
datefmt="[%X]",
21+
handlers=[RichHandler()]
22+
)
1723
logger = logging.getLogger("learn_mcp_lang")
1824

25+
# Load environment variables
1926
load_dotenv(override=True)
27+
28+
# Constants
29+
LEARN_MCP_URL = "https://learn.microsoft.com/api/mcp"
30+
31+
# Configure chat client based on API_HOST
2032
API_HOST = os.getenv("API_HOST", "github")
2133

2234
if API_HOST == "azure":
@@ -44,11 +56,17 @@
4456
)
4557

4658

47-
async def http_mcp_example():
59+
async def http_mcp_example() -> None:
60+
"""
61+
Demonstrate MCP integration with Microsoft Learn documentation.
62+
63+
Creates an agent that can answer questions about Microsoft documentation
64+
using the Microsoft Learn MCP server.
65+
"""
4866
async with (
4967
MCPStreamableHTTPTool(
5068
name="Microsoft Learn MCP",
51-
url="https://learn.microsoft.com/api/mcp",
69+
url=LEARN_MCP_URL,
5270
headers={"Authorization": "Bearer your-token"},
5371
) as mcp_server,
5472
ChatAgent(
@@ -57,11 +75,10 @@ async def http_mcp_example():
5775
instructions="You help with Microsoft documentation questions.",
5876
) as agent,
5977
):
60-
result = await agent.run(
61-
"How to create an Azure storage account using az cli?",
62-
tools=mcp_server
63-
)
78+
query = "How to create an Azure storage account using az cli?"
79+
result = await agent.run(query, tools=mcp_server)
6480
print(result)
6581

82+
6683
if __name__ == "__main__":
6784
asyncio.run(http_mcp_example())

langchainv1_mcp_http.py

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,40 @@
11
import asyncio
22
import logging
33
import os
4+
from datetime import datetime
45

56
import azure.identity
67
from dotenv import load_dotenv
78
from langchain.agents import create_agent
8-
from langchain_core.messages import HumanMessage
9+
from langchain_core.messages import HumanMessage, SystemMessage
910
from langchain_mcp_adapters.client import MultiServerMCPClient
1011
from langchain_openai import AzureChatOpenAI, ChatOpenAI
1112
from pydantic import SecretStr
1213
from rich.logging import RichHandler
1314

14-
logging.basicConfig(level=logging.WARNING, format="%(message)s", datefmt="[%X]", handlers=[RichHandler()])
15+
# Configure logging
16+
logging.basicConfig(
17+
level=logging.WARNING,
18+
format="%(message)s",
19+
datefmt="[%X]",
20+
handlers=[RichHandler()]
21+
)
1522
logger = logging.getLogger("itinerario_lang")
1623

24+
# Load environment variables
1725
load_dotenv(override=True)
26+
27+
# Constants
28+
MCP_SERVER_URL = "http://localhost:8000/mcp/"
29+
AZURE_COGNITIVE_SERVICES_SCOPE = "https://cognitiveservices.azure.com/.default"
30+
31+
# Configure language model based on API_HOST
1832
API_HOST = os.getenv("API_HOST", "github")
1933

2034
if API_HOST == "azure":
2135
token_provider = azure.identity.get_bearer_token_provider(
22-
azure.identity.DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default"
36+
azure.identity.DefaultAzureCredential(),
37+
AZURE_COGNITIVE_SERVICES_SCOPE
2338
)
2439
base_model = AzureChatOpenAI(
2540
azure_endpoint=os.environ.get("AZURE_OPENAI_ENDPOINT"),
@@ -43,38 +58,43 @@
4358
base_model = ChatOpenAI(model=os.getenv("OPENAI_MODEL", "gpt-4o-mini"))
4459

4560

46-
async def run_agent():
47-
from datetime import datetime
48-
from langchain_core.messages import SystemMessage
49-
61+
async def run_agent() -> None:
62+
"""
63+
Run the agent to process expense-related queries using MCP tools.
64+
"""
65+
# Initialize MCP client
5066
client = MultiServerMCPClient(
5167
{
52-
"itinerary": {
53-
"url": "http://localhost:8000/mcp/",
68+
"expenses": {
69+
"url": MCP_SERVER_URL,
5470
"transport": "streamable_http",
5571
}
5672
}
5773
)
5874

75+
# Get tools and create agent
5976
tools = await client.get_tools()
6077
agent = create_agent(base_model, tools)
6178

79+
# Prepare query with context
6280
today = datetime.now().strftime("%Y-%m-%d")
63-
user_query = (
64-
"yesterday I bought a laptop for $1200 using my visa. "
65-
)
81+
user_query = "yesterday I bought a laptop for $1200 using my visa."
6682

83+
# Invoke agent
6784
response = await agent.ainvoke({
6885
"messages": [
6986
SystemMessage(content=f"Today's date is {today}."),
7087
HumanMessage(content=user_query)
7188
]
7289
})
73-
final = response["messages"][-1].content
74-
print(final)
90+
91+
# Display result
92+
final_response = response["messages"][-1].content
93+
print(final_response)
7594

7695

77-
def main():
96+
def main() -> None:
97+
"""Main entry point for the application."""
7898
asyncio.run(run_agent())
7999

80100

0 commit comments

Comments
 (0)