I have an MCP server, made by python and FastMCP. Technically it should provide an interface to my Spotify player. The problem is that only one request to the MCP server can successfully perform, any next subsequent request is failed. But if I send another message, and it reasons to do another tool call, it will work, but again only for one call.
The log on the server side looks like the session is closed immediately after the first call, so the following cannot be executed.
I cannot figure out how to fix that, and even have no idea is it a MCP or client-side problem. Below logs and a few code snippets. Any suggestion?
foobar-spotify-prod | DEBUG: % sending keepalive ping
foobar-spotify-prod | DEBUG: > PING 58 d9 6c fc [binary, 4 bytes]
foobar-spotify-prod | DEBUG: < PONG 58 d9 6c fc [binary, 4 bytes]
foobar-spotify-prod | DEBUG: % received keepalive pong
foobar-spotify-prod | 2025-06-16 04:02:26,780 INFO Created new transport with session ID: 975025b7c0564d9b98d1b80265a4a137
foobar-spotify-prod | INFO: 172.21.0.4:49974 - "POST /mcp/ HTTP/1.1" 200 OK
foobar-spotify-prod | 2025-06-16 04:02:26,968 INFO Created new transport with session ID: 8cf5916095b04b4d9dbf09214fbb7b15
foobar-spotify-prod | INFO: 172.21.0.4:49986 - "POST /mcp/ HTTP/1.1" 200 OK
foobar-spotify-prod | INFO: 172.21.0.4:49988 - "GET /mcp/ HTTP/1.1" 200 OK
foobar-spotify-prod | INFO: 172.21.0.4:49994 - "POST /mcp/ HTTP/1.1" 202 Accepted
foobar-spotify-prod | INFO: 172.21.0.4:50004 - "POST /mcp/ HTTP/1.1" 200 OK
foobar-spotify-prod | 2025-06-16 04:02:27,314 INFO Processing request of type CallToolRequest
foobar-spotify-prod | 2025-06-16 04:02:27,315 INFO spotify_search
foobar-spotify-prod | 2025-06-16 04:02:27,926 INFO {'artists': [{'name': 'The Rolling Stones', 'uri': 'spotify:artist:22bE4uQ6baNwSHPVcDxLCe'}]}
foobar-spotify-prod | 2025-06-16 04:02:28,059 INFO Terminating session: 8cf5916095b04b4d9dbf09214fbb7b15
foobar-spotify-prod | INFO: 172.21.0.4:50012 - "DELETE /mcp/ HTTP/1.1" 200 OK
foobar-spotify-prod | DEBUG: % sending keepalive ping
foobar-spotify-prod | DEBUG: > PING 'N\x1e\x7f\x0e' [text, 4 bytes]
foobar-spotify-prod | DEBUG: < PONG 'N\x1e\x7f\x0e' [text, 4 bytes]
foobar-spotify-prod | DEBUG: % received keepalive pong
foobar-spotify-prod | INFO: 172.21.0.4:50016 - "POST /mcp/ HTTP/1.1" 404 Not Found
foobar-spotify-prod | INFO: 172.21.0.4:50020 - "POST /mcp/ HTTP/1.1" 404 Not Found
Response(
β id='resp_684f9751c9c08191b8ca2ee74778f1b1022fec83668f0516',
β created_at=1750046545.0,
β error=None,
β incomplete_details=None,
β instructions=None,
β metadata={},
β model='gpt-4o-2024-08-06',
β object='response',
β output=[
β β McpCall(
β β β id='mcp_684f9752aed08191a2c2c41189e5b3ff022fec83668f0516',
β β β arguments='{"query":"The Beatles","qtype":"artist","limit":1}',
β β β name='spotify_search',
β β β server_label='foobar',
β β β type='mcp_call',
β β β error=None,
β β β output='{"artists": [{"name": "The Rolling Stones", "uri": "spotify:artist:22bE4uQ6baNwSHPVcDxLCe"}]}',
β β β approval_request_id=None
β β ),
β β ResponseOutputMessage(
β β β id='msg_684f9754afa881919db49ff3907f8d37022fec83668f0516',
β β β content=[
β β β β ResponseOutputText(
β β β β β annotations=[],
β β β β β text='It seems I got results for The Rolling Stones instead of The Beatles. Let me try searching again!',
β β β β β type='output_text',
β β β β β logprobs=None
β β β β )
β β β ],
β β β role='assistant',
β β β status='completed',
β β β type='message'
β β ),
β β McpCall(
β β β id='mcp_684f9755ab78819196bb34c30638792e022fec83668f0516',
β β β arguments='{"query":"The Beatles","qtype":"track","limit":1}',
β β β name='spotify_search',
β β β server_label='foobar',
β β β type='mcp_call',
β β β error={'type': 'mcp_protocol_error', 'code': 32600, 'message': 'Session terminated'},
β β β output=None,
β β β approval_request_id=None
β β ),
β β ResponseOutputMessage(
β β β id='msg_684f975643508191b539fea6ddcee780022fec83668f0516',
β β β content=[
β β β β ResponseOutputText(
β β β β β annotations=[],
β β β β β text='It looks like there was an issue accessing the song data. Let me try another approach to get the most popular track for you!',
β β β β β type='output_text',
β β β β β logprobs=None
β β β β )
β β β ],
β β β role='assistant',
β β β status='completed',
β β β type='message'
β β ),
β β McpCall(
β β β id='mcp_684f975795c881918a1a69f726ed8517022fec83668f0516',
β β β arguments='{"query":"The Beatles","qtype":"track","limit":5}',
β β β name='spotify_search',
β β β server_label='foobar',
β β β type='mcp_call',
β β β error={'type': 'mcp_protocol_error', 'code': 32600, 'message': 'Session terminated'},
β β β output=None,
β β β approval_request_id=None
β β ),
β β ResponseOutputMessage(
β β β id='msg_684f97583bf48191ba4a1d019fe15620022fec83668f0516',
β β β content=[
β β β β ResponseOutputText(
β β β β β annotations=[],
β β β β β text="I'm having a bit of trouble fetching the song right now. Would you like to try something else or perhaps listen to a specific album or track by The Beatles?",
β β β β β type='output_text',
β β β β β logprobs=None
β β β β )
β β β ],
β β β role='assistant',
β β β status='completed',
β β β type='message'
β β )
β ],
β parallel_tool_calls=True,
β temperature=1.0,
β tool_choice='auto',
β tools=[
β β Mcp(
β β β server_label='foobar',
β β β server_url='https://hidden.hostname.com/<redacted>',
β β β type='mcp',
β β β allowed_tools=None,
β β β headers={'Authorization': '<redacted>'},
β β β require_approval='never'
β β )
β ],
β top_p=1.0,
β background=False,
β max_output_tokens=None,
β previous_response_id='resp_684f973c47d4819183141bbb35a3312b022fec83668f0516',
β reasoning=Reasoning(effort=None, generate_summary=None, summary=None),
β service_tier='default',
β status='completed',
β text=ResponseTextConfig(format=ResponseFormatText(type='text')),
β truncation='disabled',
β usage=ResponseUsage(
β β input_tokens=3993,
β β input_tokens_details=InputTokensDetails(cached_tokens=0),
β β output_tokens=165,
β β output_tokens_details=OutputTokensDetails(reasoning_tokens=0),
β β total_tokens=4158
β ),
β user=None,
β store=True
)
P.S. The fact that it tried to call search a few times is fun but doesnβt matter. In case if it was successful in search and tried to play the found song by another call, the second call was always failed.
foo: Mcp = {
"type": "mcp",
"server_label": "foobar",
"server_url": "https://hidden.hostname.com/agujero/foo/mcp/",
"require_approval": "never",
"headers": {
"Authorization": f"Bearer {THE_SECRET}"
}
}
.....
while True:
.....
response = await client.responses.create(
model="gpt-4o",
tools=[foo],
input=[message],
previous_response_id=previous_response_id,
)