forked from livekit/agents
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmcp-agent.py
More file actions
68 lines (54 loc) · 2.36 KB
/
Copy pathmcp-agent.py
File metadata and controls
68 lines (54 loc) · 2.36 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
import logging
from datetime import datetime
from dotenv import load_dotenv
from livekit.agents import Agent, AgentServer, AgentSession, JobContext, cli, inference, mcp
from livekit.agents.llm import ToolFlag
logger = logging.getLogger("mcp-agent")
load_dotenv()
class MyAgent(Agent):
def __init__(self) -> None:
super().__init__(
instructions=(
"You are a friendly travel assistant that communicates via voice. "
"Avoid emojis and markdown — speak naturally and concisely. "
"Use the get_weather tool when the user asks about the weather somewhere. "
"Use the book_flight tool when the user wants to book a flight; it takes "
"a couple of minutes and will report progress along the way — narrate the "
"updates naturally and don't make up flight details. "
"Don't repeat information you have already said in the conversation. "
f"Today is {datetime.now().strftime('%Y-%m-%d %A')}"
),
)
async def on_enter(self):
# when the agent is added to the session, it'll generate a reply
# according to its instructions
self.session.generate_reply(instructions="greeting the user and introducing yourself")
server = AgentServer()
@server.rtc_session()
async def entrypoint(ctx: JobContext):
session = AgentSession(
stt=inference.STT("deepgram/nova-3", language="multi"),
llm=inference.LLM("openai/gpt-4.1-mini"),
tts=inference.TTS("cartesia/sonic-3"),
tools=[
mcp.MCPToolset(
id="mcp_toolset_1",
mcp_server=mcp.MCPServerHTTP(
url="http://localhost:8000/sse",
# book_flight takes ~70s; bump the per-request timeout above that.
client_session_timeout_seconds=120,
),
# book_flight is long-running: forward progress and allow cancellation
tool_options={
"book_flight": {
"flags": ToolFlag.CANCELLABLE,
"on_duplicate": "confirm",
"report_progress": True,
},
},
)
],
)
await session.start(agent=MyAgent(), room=ctx.room)
if __name__ == "__main__":
cli.run_app(server)