You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am working on getting a functionality using mcp tool where i need pass the raw data at client and retrieve it as chunk with configurable Streamable-http. Below is the code for my poc:-
server.py
import asyncio
from typing import AsyncGenerator
from fastmcp import FastMCP
mcp = FastMCP(name="MyServer")
@mcp.tool()
async def generateReport(params: dict = None) -> AsyncGenerator[str, None]:
"""Generate a report with streaming response using yield.
This processes the raw_data and returns it in chunks for streaming.
"""
# Convert the raw data to string format
raw_data_str = str(params)
# Define chunk size
chunk_size = 100
# Stream the content in chunks with index
chunk_index = 0
for i in range(0, len(raw_data_str), chunk_size):
chunk = raw_data_str[i:i + chunk_size]
yield f"chunk {chunk_index}: {chunk}\n"
chunk_index += 1
# Add delay to simulate processing time
await asyncio.sleep(0.05)
if name == "main":
mcp.run(transport="streamable-http",
host="127.0.0.1",
port=8000)
democlient.py:-
import asyncio
from fastmcp import Client # type: ignore
from fastmcp.client.transports import StreamableHttpTransport # type: ignore
async def example():
transport = StreamableHttpTransport("http://127.0.0.1:8000/mcp/")
async with Client(transport=transport) as client:
await client.ping()
print("Ping is successful !")
async def render_to_output():
"""UI Client like function to fetch and render the streamed response."""
transport = StreamableHttpTransport("http://127.0.0.1:8000/mcp/")
async with Client(transport=transport) as client:
print("\n" + "="*60)
print("RENDERING STREAMED RESPONSE TO OUTPUT")
print("="*60)
Uh oh!
There was an error while loading. Please reload this page.
Hi ,
I am working on getting a functionality using mcp tool where i need pass the raw data at client and retrieve it as chunk with configurable Streamable-http. Below is the code for my poc:-
server.py
import asyncio
from typing import AsyncGenerator
from fastmcp import FastMCP
mcp = FastMCP(name="MyServer")
@mcp.tool()
async def generateReport(params: dict = None) -> AsyncGenerator[str, None]:
"""Generate a report with streaming response using yield.
if name == "main":
mcp.run(transport="streamable-http",
host="127.0.0.1",
port=8000)
democlient.py:-
import asyncio
from fastmcp import Client # type: ignore
from fastmcp.client.transports import StreamableHttpTransport # type: ignore
async def example():
transport = StreamableHttpTransport("http://127.0.0.1:8000/mcp/")
async with Client(transport=transport) as client:
await client.ping()
print("Ping is successful !")
async def render_to_output():
"""UI Client like function to fetch and render the streamed response."""
transport = StreamableHttpTransport("http://127.0.0.1:8000/mcp/")
async with Client(transport=transport) as client:
print("\n" + "="*60)
print("RENDERING STREAMED RESPONSE TO OUTPUT")
print("="*60)
if name == "main":
asyncio.run(example())
asyncio.run(render_to_output())
Getting output as below instead of raw data as incremental text chunks
Received non-streaming response: [TextContext(type="text", text="<async_generator object generateReport" at 0x0001C5809504480", annotations= None]"
Any help would be highly appreciated?
The text was updated successfully, but these errors were encountered: