Streaming End-to-End MCP Tool Use with an AI Model
See more AI Examples
Demonstrates Ai.UseMcp in a streaming Model Context Protocol (MCP) workflow. This is the same idea as the non-streaming MCP example, but the answer is streamed token-by-token and the automatic MCP tool call is driven by the application's event loop rather than happening invisibly inside Ask.
A tool-using conversation spans more than one streamed turn. In the first turn the model streams a js_function_call event; the example hands that event's JSON to StreamingJsToolCall, which routes MCP tools to the server and appends the result to the transcript, and the turn ends with null_terminator. The example then calls Ask again (with no new input, since the tool result is already in the transcript) so the model can continue and stream its final answer. The loop ends when a streamed turn finishes with no tool call outstanding.
Tip: Code to parse the returned JSON can be generated with Chilkat's online tool at https://tools.chilkat.io/jsonParse.
PollAi and reads events with NextAiEvent: a js_function_call event carries a tool request, empty means nothing to report this poll, and null_terminator marks the end of a streamed turn. The Mcp object must stay connected and in scope for the whole conversation. This example uses DeepWiki, a free, public, no-authentication Streamable HTTP MCP server; swap in any URL, setting AuthToken before Connect if a bearer token is required.UseMcp once per server — one call per MCP server — giving each a distinct prefix (for example deepwiki, weather, github) so tools from different servers never collide.Chilkat Tcl Downloads
load ./chilkat.dll
set success 0
# Streaming, end-to-end MCP example. Same idea as the non-streaming MCP example, but the answer is
# streamed token-by-token, and the automatic MCP tool call is driven by the application's event loop
# instead of happening invisibly inside Ask.
#
# A tool-using conversation spans more than one streamed turn:
#
# turn 1: the model streams a "js_function_call" event (it wants a tool). We hand that event's
# JSON to StreamingJsToolCall, which routes MCP tools to the server (Mcp.CallTool) and
# appends the result to the transcript. The turn then ends with "null_terminator".
# turn 2: we call Ask again (no new input -- the tool result is already in the transcript) so the
# model can continue. It streams the final answer and ends with "null_terminator", with
# no pending tool call.
#
# The loop ends when a streamed turn finishes with no tool call outstanding.
#
# MCP server: DeepWiki -- a free, public, no-authentication Streamable HTTP server that answers
# questions about public GitHub repositories. Swap in any URL; set mcp.AuthToken before Connect if
# it needs a bearer token.
# 1) Connect to the MCP server.
set mcp [new_CkMcp]
# mcp.AuthToken = "..."; // <-- only if the server requires a bearer token
set success [CkMcp_Connect $mcp "https://mcp.deepwiki.com/mcp"]
if {$success == 0} then {
puts [CkMcp_lastErrorText $mcp]
delete_CkMcp $mcp
exit
}
puts "Connected to MCP server: [CkMcp_serverName $mcp] (version [CkMcp_serverVersion $mcp])"
# 2) Set up the AI conversation and register the MCP tools.
set ai [new_CkAi]
CkAi_put_Provider $ai "anthropic"
# The API key should come from a secure source rather than being hard-coded.
CkAi_put_ApiKey $ai "AI_PROVIDER_API_KEY"
CkAi_put_Model $ai "claude-sonnet-5"
# Automatic tool use requires a conversation (not a stateless query).
set success [CkAi_NewConvo $ai "test_conversation" "You are a helpful assistant. Use the available tools when they can help answer the question." ""]
if {$success == 0} then {
puts [CkAi_lastErrorText $ai]
delete_CkMcp $mcp
delete_CkAi $ai
exit
}
# Namespace the server's tools with "deepwiki" -- e.g. "deepwiki_ask_question". The Mcp object must
# stay connected and in scope for the whole conversation.
set success [CkAi_UseMcp $ai $mcp "deepwiki"]
if {$success == 0} then {
puts [CkAi_lastErrorText $ai]
delete_CkMcp $mcp
delete_CkAi $ai
exit
}
# Multiple MCP servers can be used in the same conversation. Connect each server and call UseMcp
# once per server, giving each a distinct prefix so their tool names cannot collide. For example:
# success = ai.UseMcp(mcpWeather,"weather");
# success = ai.UseMcp(mcpGitHub,"github");
set success [CkAi_InputAddText $ai "Use the available tools to look up the GitHub repository \"modelcontextprotocol/modelcontextprotocol\", then give me a brief, sourced summary of which transports the Model Context Protocol defines."]
if {$success == 0} then {
puts [CkAi_lastErrorText $ai]
delete_CkMcp $mcp
delete_CkAi $ai
exit
}
# 3) Start streaming.
CkAi_put_Streaming $ai 1
set success [CkAi_Ask $ai "text"]
if {$success == 0} then {
puts [CkAi_lastErrorText $ai]
delete_CkMcp $mcp
delete_CkAi $ai
exit
}
# 4) Drive the streaming event loop.
set sbEventName [new_CkStringBuilder]
set sbDelta [new_CkStringBuilder]
set sbFullResponse [new_CkStringBuilder]
set bConversationDone 0
set bToolCallPending 0
set bCaseSensitive 1
set bAbort 0
set loopGuard 0
set result 0
while {expr [$bConversationDone != 1] && [$loopGuard < 8000]} {
set loopGuard [expr $loopGuard + 1]
set result [CkAi_PollAi $ai $bAbort]
if {$result < 0} then {
puts [CkAi_lastErrorText $ai]
delete_CkMcp $mcp
delete_CkAi $ai
delete_CkStringBuilder $sbEventName
delete_CkStringBuilder $sbDelta
delete_CkStringBuilder $sbFullResponse
exit
}
if {$result == 0} then {
# No event ready yet.
CkAi_SleepMs $ai 100
} else {
set success [CkAi_NextAiEvent $ai 5000 $sbEventName $sbDelta]
if {$success == 0} then {
puts [CkAi_lastErrorText $ai]
delete_CkMcp $mcp
delete_CkAi $ai
delete_CkStringBuilder $sbEventName
delete_CkStringBuilder $sbDelta
delete_CkStringBuilder $sbFullResponse
exit
}
if {[CkStringBuilder_ContentsEqual $sbEventName "js_function_call" $bCaseSensitive] == 1} then {
# (a) The model is requesting a tool call. sbDelta holds the function-call JSON.
# StreamingJsToolCall runs the tool -- routing MCP tools to the server -- and appends the
# result to the transcript. It must be called before this turn's null_terminator.
puts "{[tool call requested]}"
puts [CkStringBuilder_getAsString $sbDelta]
set success [CkAi_StreamingJsToolCall $ai $sbDelta]
if {$success == 0} then {
puts [CkAi_lastErrorText $ai]
delete_CkMcp $mcp
delete_CkAi $ai
delete_CkStringBuilder $sbEventName
delete_CkStringBuilder $sbDelta
delete_CkStringBuilder $sbFullResponse
exit
}
set bToolCallPending 1
} else {
if {[CkStringBuilder_ContentsEqual $sbEventName "null_terminator" $bCaseSensitive] == 1} then {
# (c) This streamed turn has finished.
if {$bToolCallPending == 1} then {
# Continue the conversation so the model can use the tool result. No new
# InputAddText -- the tool result is already in the transcript.
set bToolCallPending 0
set success [CkAi_Ask $ai "text"]
if {$success == 0} then {
puts [CkAi_lastErrorText $ai]
delete_CkMcp $mcp
delete_CkAi $ai
delete_CkStringBuilder $sbEventName
delete_CkStringBuilder $sbDelta
delete_CkStringBuilder $sbFullResponse
exit
}
} else {
set bConversationDone 1
}
} else {
# (b) An "empty" event reports nothing this poll. (d) Any other event is a normal
# streamed text delta, which we display and accumulate.
if {[CkStringBuilder_ContentsEqual $sbEventName "empty" $bCaseSensitive] != 1} then {
puts "Event: [CkStringBuilder_getAsString $sbEventName] Delta: [CkStringBuilder_getAsString $sbDelta]"
CkStringBuilder_AppendSb $sbFullResponse $sbDelta
}
}
}
}
}
puts "Final streamed response:"
puts [CkStringBuilder_getAsString $sbFullResponse]
puts "----"
# (Optional) The full transcript, including the tool call and its result.
set convoJson [new_CkJsonObject]
CkJsonObject_put_EmitCompact $convoJson 0
set success [CkAi_ExportConvo $ai "test_conversation" $convoJson]
if {$success == 0} then {
puts [CkAi_lastErrorText $ai]
delete_CkMcp $mcp
delete_CkAi $ai
delete_CkStringBuilder $sbEventName
delete_CkStringBuilder $sbDelta
delete_CkStringBuilder $sbFullResponse
delete_CkJsonObject $convoJson
exit
}
puts "Full Conversation:"
puts [CkJsonObject_emit $convoJson]
# 5) Close the MCP session.
set success [CkMcp_Close $mcp]
if {$success == 0} then {
puts [CkMcp_lastErrorText $mcp]
delete_CkMcp $mcp
delete_CkAi $ai
delete_CkStringBuilder $sbEventName
delete_CkStringBuilder $sbDelta
delete_CkStringBuilder $sbFullResponse
delete_CkJsonObject $convoJson
exit
}
delete_CkMcp $mcp
delete_CkAi $ai
delete_CkStringBuilder $sbEventName
delete_CkStringBuilder $sbDelta
delete_CkStringBuilder $sbFullResponse
delete_CkJsonObject $convoJson