I'm working on a project where I'm trying to implement a model context protocol server for my LLM to use called server-sequential-thinking. I have a chat area where a user can enter a prompt and the LLM I am using will use the tools from the server to give a detailed response however my frontend is only displaying the first thought then completing. I added a console.log() on the frontend and confirmed only the first thought is received.
This is my Laravel backend code:
return response()->stream(function () use ($conversation) {
$stream = Prism::text()
->using(Provider::Anthropic, 'claude-3-7-sonnet-latest')
->withMessages($conversation)
->withTools([
...Relay::tools('sequential-thinking'),
])
->asStream();
foreach ($stream as $response) {
if($response->chunkType === ChunkType::ToolCall){
echo "event: toolcall\n";
echo "data: " . json_encode($response->toolCalls, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . "\n\n";
}
elseif($response->chunkType === ChunkType::ToolResult){
echo "event: toolresult\n";
echo "data: " . json_encode($response->toolResults, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . "\n\n";
}
elseif($response->chunkType === ChunkType::Text){
echo "data: " . str_replace("\n", "\\n", $response->text) . "\n\n";
}
ob_flush();
flush();
}
echo "data: complete\n\n";
ob_flush();
flush();
}, 200, [
'Content-Type' => 'text/event-stream',
'Cache-Control' => 'no-cache',
'Connection' => 'keep-alive',
'X-Accel-Buffering' => 'no',
]);
This is the frontend where I implemented an event listener:
source.addEventListener('toolresult', (e)=>{
const toolResult = JSON.parse(e.data);
const [{args: {thought, thoughtNumber, totalThoughts}}] = toolResult;
console.log(`Thought: ${thought}\nThought Number: ${thoughtNumber}\nTotal Thoughts: ${totalThoughts}`);
prismText.value += thought;
console.log("Tool result: ",toolResult)
})
Here is what the browser console looks like when I ask a prompt
I am using Prism Relay to add the MCP server
How can I properly have all thoughts displayed on the frontend?