forked from microsoft/mcp-for-beginners
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscenario1.py
More file actions
88 lines (77 loc) · 3.64 KB
/
scenario1.py
File metadata and controls
88 lines (77 loc) · 3.64 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# Scenario 1: Simple query to Docs MCP
# This script demonstrates how to connect to the Microsoft Learn Docs MCP server,
# send a query, and print the result.
import asyncio
import logging
import sys
from mcp.client.streamable_http import streamablehttp_client
from mcp import ClientSession
import json
# Microsoft Docs MCP Server Endpoint
MCP_SERVER_URL = "https://learn.microsoft.com/api/mcp"
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S'
)
logger = logging.getLogger('mcp_client')
def prompt_user():
"""Prompt user for a search query."""
print("Type your Microsoft Docs search query (or 'exit' to quit):")
try:
return input("> ").strip()
except (KeyboardInterrupt, EOFError):
print("\nDetected exit signal.")
return "exit"
async def main():
logger.info("Connecting to Microsoft Docs MCP Server at: %s", MCP_SERVER_URL)
try:
async with streamablehttp_client(MCP_SERVER_URL) as (read_stream, write_stream, _):
logger.info("Connection established. Initializing MCP session.")
async with ClientSession(read_stream, write_stream) as session:
await session.initialize()
logger.info("Session initialized successfully.")
print("\n=== Microsoft Docs MCP Client ===")
print("Tool: microsoft_docs_search")
print("Enter documentation queries to search Microsoft Learn docs.")
print("Type 'exit' or 'quit' to end the session.\n")
while True:
user_query = prompt_user()
if not user_query:
print("Query cannot be empty. Please try again.")
continue
if user_query.lower() in ("exit", "quit"):
print("Exiting client. Goodbye!")
break
try:
logger.info("Executing query: %s", user_query)
result = await session.call_tool("microsoft_docs_search", {"question": user_query})
# print("RESULT:", result)
print("\n--- Search Result ---")
if hasattr(result, 'content'):
# print(result.content)
for item in result.content:
my_list = json.loads(item.text)
for doc in my_list:
print(f"[Title]: {doc.get('title', 'No title')}")
print(f"[Content]: {doc.get('content', 'No content')}")
print("---")
else:
print("No content returned from the search.")
print("---------------------\n")
except Exception as e:
logger.error("Query failed: %s", e)
print(f"Error: {e}. Please try a different query or check your connection.\n")
except Exception as e:
logger.error("Connection error: %s", e)
print(f"Failed to connect to Microsoft Docs MCP Server: {e}")
print("Please check your internet connection and try again.")
sys.exit(1)
if __name__ == "__main__":
print("Starting Microsoft Docs MCP Client...")
try:
asyncio.run(main())
except KeyboardInterrupt:
print("\nApplication terminated by user.")
sys.exit(0)