Notikumi MCP balss sarunās
Šajā lapā
MCP balss aģentā
Model Context Protocol (MCP) ļauj tavam balss aģentam pieslēgties ārējiem rīku serveriem, izmantojot standartizētu protokolu. Konfigurējot MCP rīku savā sesijā, Voice Agent API ģenerē specifiskus notikumus katrai mijiedarbības darbībai ar MCP serveri.
MCP rīka konfigurēšana
Lai aktivizētu MCP serveri savā balss sesijā, pievieno to rīku sarakstam:
{
"type": "session.update",
"session": {
"tools": [
{
"type": "mcp",
"server_url": "https://mon-serveur-mcp.exemple.com/sse",
"server_label": "Internal CRM"
}
]
}
}
Lauks server_url norāda uz tavu MCP serveri, bet server_label ir lasāms nosaukums, kas identificē serveri žurnālos.
Rīku atklāšana
Kad aģents izveido savienojumu ar MCP serveri, tas vispirms iegūst pieejamo rīku sarakstu. Šo posmu iezīmē trīs notikumi.
mcp_list_tools.in_progress
{
"type": "mcp_list_tools.in_progress",
"server_label": "Internal CRM"
}
Notiek MCP servera aptauja, lai iegūtu tā rīku sarakstu.
mcp_list_tools.completed
{
"type": "mcp_list_tools.completed",
"server_label": "Internal CRM",
"tools": [
{
"name": "find_customer",
"description": "Finds a customer by name or email"
},
{
"name": "create_ticket",
"description": "Creates a support ticket"
}
]
}
Rīku saraksts ir veiksmīgi iegūts. Tagad balss aģents var tos izmantot sarunā.
mcp_list_tools.failed
{
"type": "mcp_list_tools.failed",
"server_label": "Internal CRM",
"error": {
"message": "Connection timeout"
}
}
Savienojums ar MCP serveri neizdevās. Aģents turpinās sarunu bez šiem rīkiem.
MCP rīku izsaukumi
Kad aģents sarunas laikā nolemj izmantot MCP rīku, tiek ģenerēti papildu notikumi.
response.mcp_call_arguments.delta / .done
Tāpat kā parastajos funkciju izsaukumu notikumos, šie notikumi straumē izsaukuma argumentus:
{
"type": "response.mcp_call_arguments.delta",
"delta": "{\"name\": \"Mil"
}
{
"type": "response.mcp_call_arguments.done",
"arguments": "{\"name\": \"Miller\"}"
}
response.mcp_call.in_progress / .completed / .failed
Šie notikumi izseko izsaukuma izpildi MCP serverī:
{
"type": "response.mcp_call.in_progress",
"server_label": "Internal CRM",
"tool_name": "find_customer"
}
{
"type": "response.mcp_call.completed",
"server_label": "Internal CRM",
"tool_name": "find_customer",
"result": "{\"id\": 42, \"name\": \"Miller\", \"email\": \"[email protected]\"}"
}
{
"type": "response.mcp_call.failed",
"server_label": "Internal CRM",
"tool_name": "find_customer",
"error": {
"message": "Customer not found"
}
}
Atšķirības no parastās funkciju izsaukšanas
MCP rīki darbojas servera pusē, izmantojot xAI infrastruktūru, tāpēc tev pašam nav jānodrošina to izpilde. Šī ir galvenā atšķirība no parastās funkciju izsaukšanas, kur tavai lietotnei ir jāizpilda funkcija un jānosūta rezultāts atpakaļ.
| Aspekts | Funkciju izsaukšana | MCP |
|---|---|---|
| Izpilde | Tava lietotne | Attālais MCP serveris |
| Rezultāts | Tu to nosūti atpakaļ | Automātiski |
| Latence | Atkarīga no tava koda | Atkarīga no MCP servera |
| Konfigurācija | Lokāla JSON shēma | MCP servera URL |
MCP notikumu apstrāde tavā lietotnē
Lūk, piemērs pilnīgai MCP notikumu apstrādei:
ws.on("message", (data) => {
const event = JSON.parse(data);
switch (event.type) {
case "mcp_list_tools.in_progress":
console.log(`Connecting to MCP server: ${event.server_label}`);
break;
case "mcp_list_tools.completed":
console.log(`Available tools: ${event.tools.length}`);
break;
case "mcp_list_tools.failed":
console.error(`MCP error: ${event.error.message}`);
break;
case "response.mcp_call.in_progress":
showToolIndicator(event.tool_name);
break;
case "response.mcp_call.completed":
hideToolIndicator();
break;
case "response.mcp_call.failed":
hideToolIndicator();
showError(`Tool ${event.tool_name} unavailable`);
break;
}
});
Galvenās atziņas
- MCP rīki ļauj balss aģentam strādāt ar ārējiem serveriem, izmantojot standartizētu protokolu
- Rīku atklāšana notiek automātiski, izmantojot
mcp_list_toolsnotikumus - MCP izsaukumi darbojas servera pusē, atšķirībā no parastās funkciju izsaukšanas
- Katrs izsaukums iziet cauri trim iespējamiem stāvokļiem:
in_progress, pēc tamcompletedvaifailed - Apstrādā MCP savienojuma kļūmes korekti: aģents var turpināt darbu bez šiem rīkiem