Skip to content

Commit 4d85c12

Browse files
mcp powered voice agents (#1897)
1 parent cc63685 commit 4d85c12

14 files changed

+1098
-0
lines changed
Binary file not shown.

examples/partners/mcp_powered_voice_agents/mcp_powered_agents_cookbook.ipynb

Lines changed: 976 additions & 0 deletions
Large diffs are not rendered by default.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
import os
2+
from mcp.server.fastmcp import FastMCP
3+
from openai import OpenAI
4+
from agents import set_tracing_export_api_key
5+
6+
# Create server
7+
mcp = FastMCP("Search Server")
8+
_vector_store_id = ""
9+
10+
def _run_rag(query: str) -> str:
11+
"""Do a search for answers within the knowledge base and internal documents of the user.
12+
Args:
13+
query: The user query
14+
"""
15+
results = client.vector_stores.search(
16+
vector_store_id=_vector_store_id,
17+
query=query,
18+
rewrite_query=True, # Query rewriting generally improves results
19+
)
20+
return results.data[0].content[0].text
21+
22+
23+
def _summarize_rag_response(rag_output: str) -> str:
24+
"""Summarize the RAG response using GPT-4
25+
Args:
26+
rag_output: The RAG response
27+
"""
28+
response = client.responses.create(
29+
model="gpt-4.1-mini",
30+
tools=[{"type": "web_search_preview"}],
31+
input="Summarize the following text concisely: \n\n" + rag_output,
32+
)
33+
return response.output_text
34+
35+
36+
@mcp.tool()
37+
def generate_rag_output(query: str) -> str:
38+
"""Generate a summarized RAG output for a given query.
39+
Args:
40+
query: The user query
41+
"""
42+
print("[debug-server] generate_rag_output: ", query)
43+
rag_output = _run_rag(query)
44+
return _summarize_rag_response(rag_output)
45+
46+
47+
@mcp.tool()
48+
def run_web_search(query: str) -> str:
49+
"""Run a web search for the given query.
50+
Args:
51+
query: The user query
52+
"""
53+
print("[debug-server] run_web_search:", query)
54+
response = client.responses.create(
55+
model="gpt-4.1-mini",
56+
tools=[{"type": "web_search_preview"}],
57+
input=query,
58+
)
59+
return response.output_text
60+
61+
62+
def index_documents(directory: str):
63+
"""Index the documents in the given directory to the vector store
64+
Args:
65+
directory: The directory to index the documents from
66+
"""
67+
# OpenAI supported file extensions for retrieval (see docs)
68+
SUPPORTED_EXTENSIONS = {'.pdf', '.txt', '.md', '.docx', '.pptx', '.csv', '.rtf', '.html', '.json', '.xml'}
69+
# Collect all files in the specified directory
70+
files = [os.path.join(directory, f) for f in os.listdir(directory)]
71+
# Filter files for supported extensions only
72+
supported_files = []
73+
for file_path in files:
74+
_, ext = os.path.splitext(file_path)
75+
if ext.lower() in SUPPORTED_EXTENSIONS:
76+
supported_files.append(file_path)
77+
else:
78+
print(f"[warning] Skipping unsupported file for retrieval: {file_path}")
79+
80+
vector_store = client.vector_stores.create( # Create vector store
81+
name="Support FAQ",
82+
)
83+
global _vector_store_id
84+
_vector_store_id = vector_store.id
85+
86+
for file_path in supported_files:
87+
# Upload each file to the vector store, ensuring the file handle is closed
88+
with open(file_path, "rb") as fp:
89+
client.vector_stores.files.upload_and_poll(
90+
vector_store_id=vector_store.id,
91+
file=fp
92+
)
93+
print(f"[debug-server] uploading file: {file_path}")
94+
95+
96+
if __name__ == "__main__":
97+
oai_api_key = os.environ.get("OPENAI_API_KEY")
98+
if not oai_api_key:
99+
raise ValueError("OPENAI_API_KEY environment variable is not set")
100+
set_tracing_export_api_key(oai_api_key)
101+
client = OpenAI(api_key=oai_api_key)
102+
103+
current_dir = os.path.dirname(os.path.abspath(__file__))
104+
samples_dir = os.path.join(current_dir, "sample_files")
105+
index_documents(samples_dir)
106+
107+
mcp.run(transport="sse")

images/System_flow_partner_mcp.png

164 KB
Loading

images/Traces-1_partner.png

613 KB
Loading

images/Traces-2_partner.png

346 KB
Loading

images/partner_mcp_Cookbook.svg

Lines changed: 1 addition & 0 deletions
Loading

images/trace-sk1_partner.png

569 KB
Loading

images/traces_partner_granular.png

513 KB
Loading

registry.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,20 @@
44
# should build pages for, and indicates metadata such as tags, creation date and
55
# authors for each page.
66

7+
- title: MCP Powered Voice Agents
8+
path: examples/partners/mcp_powered_voice_agents/mcp_powered_agents_cookbook.ipynb
9+
date: 2025-06-12
10+
authors:
11+
- shikhar-cyber
12+
- Cece Z
13+
- Sibon li
14+
tags:
15+
- mcp
16+
- voice
17+
- agents-sdk
18+
- functions
19+
- tracing
20+
721
- title: Eval Driven System Design - From Prototype to Production
822
path: examples/partners/eval_driven_system_design/receipt_inspection.ipynb
923
date: 2025-06-02

0 commit comments

Comments
 (0)