René's URL Explorer Experiment


Title: Error when calling MCP tool with arguments and tool name in live environment · Issue #662 · modelcontextprotocol/python-sdk · GitHub

Open Graph Title: Error when calling MCP tool with arguments and tool name in live environment · Issue #662 · modelcontextprotocol/python-sdk

X Title: Error when calling MCP tool with arguments and tool name in live environment · Issue #662 · modelcontextprotocol/python-sdk

Description: Describe the bug I am experiencing an issue when calling the MCP tool with arguments and tool name in the live environment using Railway. I am hosting both the FastAPI with the MCP client and the MCP server on Railway. The tool call work...

Open Graph Description: Describe the bug I am experiencing an issue when calling the MCP tool with arguments and tool name in the live environment using Railway. I am hosting both the FastAPI with the MCP client and the M...

X Description: Describe the bug I am experiencing an issue when calling the MCP tool with arguments and tool name in the live environment using Railway. I am hosting both the FastAPI with the MCP client and the M...

Opengraph URL: https://github.com/modelcontextprotocol/python-sdk/issues/662

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Error when calling MCP tool with arguments and tool name in live environment","articleBody":"**Describe the bug**\nI am experiencing an issue when calling the MCP tool with arguments and tool name in the live environment using Railway. I am hosting both the FastAPI with the MCP client and the MCP server on Railway. The tool call works fine in the local environment, but when deployed on Railway (live), the tool does not work as expected. The issue arises after the LLM response with arguments and the tool name.\n\nIn the local environment, I use the SSE protocol to interact with the MCP server and everything works fine. However, in the live environment, the tool call fails despite receiving all tools correctly from the MCP server when the app starts.\n\n**To Reproduce**\nSteps to reproduce the behavior:\n1. Deploy the FastAPI application with the MCP client and MCP server on Railway.\n2. Use the LLM response with arguments and the tool name.\n3. Observe that the tool call works on the local environment but fails in the live environment.\n\n**Expected behavior**\nThe tool call should work correctly both in the local environment and live environment. It should successfully invoke the tool when passing the correct arguments and tool name.\n\n**Screenshots**\n\n![Image](https://github.com/user-attachments/assets/50a48783-f3f3-499f-b69a-763c59161e3b)\n\n**Desktop (please complete the following information):**\n - OS: Windows 10\n - Browser chrome\n - Version 136.0.7103.93\n\n**Additional context**\n- The tool call works locally, but not in the live environment.\n- Both environments use the same configuration, but there seems to be a difference in how the SSE protocol behaves between local and live setups. \n\nhere is my code mcp client+fast api\nfrom contextlib import asynccontextmanager\nfrom fastapi import FastAPI\nfrom pydantic import BaseModel\nfrom config import SERVER_URL\nfrom mcp_client import McpClient\nfrom fastapi.middleware.cors import CORSMiddleware\nfrom fastapi import HTTPException\nfrom mcp import ClientSession\nfrom mcp.client.sse import sse_client\n\nclass QuerySchema(BaseModel):\n    query:str\n\n@asynccontextmanager\nasync def lifespan(app:FastAPI):\n    client = McpClient()\n    try:\n        connected = await client.connect_to_server('http://127.0.0.1:3000/sse')\n        # connected = await client.connect_to_server(f'{SERVER_URL}')\n        if not connected:\n             raise HTTPException(\n                status_code=500, detail=\"Failed to connect to MCP server\"\n            )\n        app.state.client = client\n        print(\"app start\")\n        # print(app.state.client)\n        yield \n    except Exception as e:\n        raise HTTPException(status_code=500, detail=\"Error during lifespan\") from e\n    finally:\n        # shutdown when app close\n        print(\"app close\")\n        await client.clenup()\n\napp = FastAPI(title=\"MCP CLIENT CALCULATOR\",lifespan=lifespan)\n\n\n# Add CORS middleware\napp.add_middleware(\n    CORSMiddleware,\n    allow_origins=[\"*\"],  # Allows all origins\n    allow_credentials=True,\n    allow_methods=[\"*\"],  # Allows all methods\n    allow_headers=[\"*\"],  # Allows all headers\n)\n\n\n\n@app.post('/query')\nasync def caluclator(request:QuerySchema):\n    try:\n        response  = await app.state.client.process_query(request.query)\n        # print (response.status_code)\n        print (\"response_tool\",response)\n        return response\n\n    except Exception as e:\n        raise HTTPException(status_code=500, detail=f\"Error {e}\") from e\n        \n\nif __name__ == \"__main__\":\n    import uvicorn    \n    uvicorn.run(app,host='0.0.0.0',port=8000)\n\nmcp_client.py file\nfrom fastapi import HTTPException\nfrom mcp import ClientSession\nfrom mcp.client.sse import sse_client\nfrom typing import Optional\nfrom contextlib import AsyncExitStack\nfrom google import genai\nfrom config import GOOGLE_API_KEY\nfrom google.genai import types\nclass McpClient:\n    def __init__(self):\n        self.tools = []\n        self.messages = []\n        self.session:Optional[ClientSession]  = None\n        self.exit_stack = AsyncExitStack()\n        self.client = genai.Client(api_key=GOOGLE_API_KEY)\n        \n    async def connect_to_server(self,path:str):\n        try:\n            read,write = await self.exit_stack.enter_async_context(sse_client(url=path))\n            self.session = await self.exit_stack.enter_async_context(ClientSession(read,write))\n            await self.session.initialize()\n            # get all the tools\n            # get all the tools\n            self.tools = await self.get_mcp_tools()\n            print(self.tools)\n            return True\n                \n        except Exception as e:\n              raise\n        \n    async def get_mcp_tools(self):\n        try:\n            # print(awaitself.session.list_tools())\n            response = await self.session.list_tools()\n            return response.tools\n        except Exception as e:\n             raise(e)      \n    \n    async def process_query(self,query:str):\n        try:\n            \"\"\" when user hit the query save in self.messages\"\"\" \n            self.messages = [types.Content(role=\"user\",parts=[types.Part(text=query)])]\n            # print()\n            # print(\"first message with query\",self.messages)\n            # print()\n            while True:\n                \"\"\"now call the call_llm function to get response from gemini\"\"\" \n                response  = await self.call_llm()\n                # print(\"response---\u003e\u003e\u003e\",response)\n                content = response.candidates[0].content\n                parts = content.parts\n                \n                # print()\n                # print(\"second message with fnction call by llm\",self.messages)\n                # print()\n                \n                # if response.candidates[0].content.parts[0].function_call:\n                if parts and parts[0].function_call:\n                    \"\"\"After call call_llm llm return the function tool that we send with useer query and all tools fetch from mcp server and now append with self.messages funtion call send by llm according to query\"\"\"\n                    self.messages.append(\n                    types.Content(role=response.candidates[0].content.role,parts=[types.Part(function_call=parts[0].function_call)])\n                    )\n                    \n                    function_call = parts[0].function_call\n                    print(f\"Function to call: {function_call.name}\")\n                    print(f\"Arguments: {function_call.args}\")\n                    try:\n                        \"\"\"After get tool send by llm now call that tool to mcp server to fetch data according to that function and append result to self.messages \"\"\"\n                        result = await self.session.call_tool(name=function_call.name,arguments=function_call.args)\n                        \n                        self.messages.append(\n                            types.Content(role=\"tool_use\",\n                            parts=[types.Part.from_function_response(\n                                name=function_call.name,\n                                response={\"result\":result}\n                            )]),\n                            \n                        )\n                        # print()\n                        # print(\"third message from mcp server tool give by llm\",self.messages)\n                        \"\"\"now finally send this self.messages list to llm to get final result that get from mcp server by calling tool purpose of this call to llm to get more understandable answer\"\"\"\n                        # print()\n                        final_result = self.client.models.generate_content(\n                            model=\"gemini-2.5-flash-preview-04-17\",\n                            config=types.GenerateContentConfig(temperature=1),\n                            contents=self.messages\n                        )\n                        self.messages.append(\n                            types.Content(role=final_result.candidates[0].content.role,parts=[types.Part(text=final_result.candidates[0].content.parts[0].text)]) \n                        )\n                        break\n                    except Exception as e:\n                        # print(f\"error get tool response of {function_call.name}\")\n                        # print(f\"error get tool response of {function_call.args}\")\n                        raise HTTPException(status_code=500,details = f\"Error calling tool {e}\")\n                else:\n                    print(\"No function call found in the response.\")\n                    print(response.candidates[0].content.parts[0].text)\n                    self.messages.append(\n                            types.Content(\n                                role=content.role,\n                                parts=[types.Part(text=response.candidates[0].content.parts[0].text)]\n                            )\n                        \n                    )\n                    break\n            return self.messages\n                    \n        except Exception as e:\n            raise(e)\n\n    async def call_llm(self):\n        \"\"\"Call llm with tools get when app start first time and with self.messages having user quer and user role\"\"\"\n        try:\n            available_tools = [\n                types.Tool(\n                    function_declarations=[\n                        {\n                            \"name\": tool.name,\n                            \"description\": tool.description,\n                            \"parameters\": {\n                                k: v\n                                for k, v in tool.inputSchema.items()\n                                if k not in [\"additionalProperties\", \"$schema\"]\n                            },\n                        }\n                    ]\n                )\n                for tool in self.tools\n            ]\n            # print(available_tools)\n            return self.client.models.generate_content(\n                    model=\"gemini-2.5-flash-preview-04-17\",\n                    config=types.GenerateContentConfig(temperature=1,tools=available_tools),\n                    contents=self.messages\n                )\n        except Exception as e:\n            raise(e)\n\n    async def call_tool(self,arguments:dict):\n        try:\n            # print(a,b,operator)\n            response  = await self.session.call_tool(name=\"mcpCalculator\",arguments=arguments)\n            return response\n\n        except Exception as e:\n             raise(e)\n    \n    async def clenup(self):\n        try:\n            await self.exit_stack.aclose()\n            print(\"Disconnected from MCP server\")\n        except Exception as e:\n            raise(e)\n","author":{"url":"https://github.com/Muhammadtalha678","@type":"Person","name":"Muhammadtalha678"},"datePublished":"2025-05-08T07:07:38.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":1},"url":"https://github.com/662/python-sdk/issues/662"}

route-pattern/_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format)
route-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:b6415dd7-4e0c-41be-6971-78e41cd4dc2d
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-id8AB0:345E8A:25ED8D:338814:6A57F90D
html-safe-nonce2742c648abc98ece4376f01972b8cca24f1c17aa4c0a729fa9cb305973f0a2f2
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4QUIwOjM0NUU4QToyNUVEOEQ6MzM4ODE0OjZBNTdGOTBEIiwidmlzaXRvcl9pZCI6IjEzMzgyMjg2NjA2NTA4OTk3MjUiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ==
visitor-hmaca59075644186fb54a47da36295b208a00f8497e6a7084e0cd00b5fdeba225525
hovercard-subject-tagissue:3048033485
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/modelcontextprotocol/python-sdk/662/issue_layout
twitter:imagehttps://opengraph.githubassets.com/5318e7d94d4d6edb368585c48ef5ed7fe1c804811daab0d670f708ed4cfd915a/modelcontextprotocol/python-sdk/issues/662
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/5318e7d94d4d6edb368585c48ef5ed7fe1c804811daab0d670f708ed4cfd915a/modelcontextprotocol/python-sdk/issues/662
og:image:altDescribe the bug I am experiencing an issue when calling the MCP tool with arguments and tool name in the live environment using Railway. I am hosting both the FastAPI with the MCP client and the M...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernameMuhammadtalha678
hostnamegithub.com
expected-hostnamegithub.com
None49c8c15fabcbf356d607a90ca115c13b273e42ff8b74155de050fd229a9b0121
turbo-cache-controlno-preview
go-importgithub.com/modelcontextprotocol/python-sdk git https://github.com/modelcontextprotocol/python-sdk.git
octolytics-dimension-user_id182288589
octolytics-dimension-user_loginmodelcontextprotocol
octolytics-dimension-repository_id862584018
octolytics-dimension-repository_nwomodelcontextprotocol/python-sdk
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id862584018
octolytics-dimension-repository_network_root_nwomodelcontextprotocol/python-sdk
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release3fb1f684e7a833eb1b2d01d39875a2b52cb4fe9b
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/modelcontextprotocol/python-sdk/issues/662#start-of-content
https://github.com/
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmodelcontextprotocol%2Fpython-sdk%2Fissues%2F662
GitHub CopilotWrite better code with AIhttps://github.com/features/copilot
GitHub Copilot appDirect agents from issue to mergehttps://github.com/features/ai/github-app
MCP RegistryNewIntegrate external toolshttps://github.com/mcp
ActionsAutomate any workflowhttps://github.com/features/actions
CodespacesInstant dev environmentshttps://github.com/features/codespaces
IssuesPlan and track workhttps://github.com/features/issues
Code ReviewManage code changeshttps://github.com/features/code-review
GitHub Advanced SecurityFind and fix vulnerabilitieshttps://github.com/security/advanced-security
Code securitySecure your code as you buildhttps://github.com/security/advanced-security/code-security
Secret protectionStop leaks before they starthttps://github.com/security/advanced-security/secret-protection
Why GitHubhttps://github.com/why-github
Documentationhttps://docs.github.com
Bloghttps://github.blog
Changeloghttps://github.blog/changelog
Marketplacehttps://github.com/marketplace
View all featureshttps://github.com/features
Enterpriseshttps://github.com/enterprise
Small and medium teamshttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
App Modernizationhttps://github.com/solutions/use-case/app-modernization
DevSecOpshttps://github.com/solutions/use-case/devsecops
DevOpshttps://github.com/solutions/use-case/devops
CI/CDhttps://github.com/solutions/use-case/ci-cd
View all use caseshttps://github.com/solutions/use-case
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
View all industrieshttps://github.com/solutions/industry
View all solutionshttps://github.com/solutions
AIhttps://github.com/resources/articles?topic=ai
Software Developmenthttps://github.com/resources/articles?topic=software-development
DevOpshttps://github.com/resources/articles?topic=devops
Securityhttps://github.com/resources/articles?topic=security
View all topicshttps://github.com/resources/articles
Customer storieshttps://github.com/customer-stories
Events & webinarshttps://github.com/resources/events
Ebooks & reportshttps://github.com/resources/whitepapers
Business insightshttps://github.com/solutions/executive-insights
GitHub Skillshttps://skills.github.com
Documentationhttps://docs.github.com
Customer supporthttps://support.github.com
Community forumhttps://github.com/orgs/community/discussions
Trust centerhttps://github.com/trust-center
Partnershttps://github.com/partners
View all resourceshttps://github.com/resources
GitHub SponsorsFund open source developershttps://github.com/open-source/sponsors
Security Labhttps://securitylab.github.com
Maintainer Communityhttps://maintainers.github.com
Acceleratorhttps://github.com/open-source/accelerator
GitHub Starshttps://stars.github.com
Archive Programhttps://archiveprogram.github.com
Topicshttps://github.com/topics
Trendinghttps://github.com/trending
Collectionshttps://github.com/collections
Enterprise platformAI-powered developer platformhttps://github.com/enterprise
GitHub Advanced SecurityEnterprise-grade security featureshttps://github.com/security/advanced-security
Copilot for BusinessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Pricinghttps://github.com/pricing
Search syntax tipshttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
documentationhttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmodelcontextprotocol%2Fpython-sdk%2Fissues%2F662
Sign up https://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=modelcontextprotocol%2Fpython-sdk
Reloadhttps://github.com/modelcontextprotocol/python-sdk/issues/662
Reloadhttps://github.com/modelcontextprotocol/python-sdk/issues/662
Reloadhttps://github.com/modelcontextprotocol/python-sdk/issues/662
Please reload this pagehttps://github.com/modelcontextprotocol/python-sdk/issues/662
modelcontextprotocol https://github.com/modelcontextprotocol
python-sdkhttps://github.com/modelcontextprotocol/python-sdk
Notifications https://github.com/login?return_to=%2Fmodelcontextprotocol%2Fpython-sdk
Fork 3.6k https://github.com/login?return_to=%2Fmodelcontextprotocol%2Fpython-sdk
Star 23.6k https://github.com/login?return_to=%2Fmodelcontextprotocol%2Fpython-sdk
Code https://github.com/modelcontextprotocol/python-sdk
Issues 251 https://github.com/modelcontextprotocol/python-sdk/issues
Pull requests 291 https://github.com/modelcontextprotocol/python-sdk/pulls
Actions https://github.com/modelcontextprotocol/python-sdk/actions
Projects https://github.com/modelcontextprotocol/python-sdk/projects
Models https://github.com/modelcontextprotocol/python-sdk/models
Security and quality 6 https://github.com/modelcontextprotocol/python-sdk/security
Insights https://github.com/modelcontextprotocol/python-sdk/pulse
Code https://github.com/modelcontextprotocol/python-sdk
Issues https://github.com/modelcontextprotocol/python-sdk/issues
Pull requests https://github.com/modelcontextprotocol/python-sdk/pulls
Actions https://github.com/modelcontextprotocol/python-sdk/actions
Projects https://github.com/modelcontextprotocol/python-sdk/projects
Models https://github.com/modelcontextprotocol/python-sdk/models
Security and quality https://github.com/modelcontextprotocol/python-sdk/security
Insights https://github.com/modelcontextprotocol/python-sdk/pulse
Error when calling MCP tool with arguments and tool name in live environmenthttps://github.com/modelcontextprotocol/python-sdk/issues/662#top
bugSomething isn't workinghttps://github.com/modelcontextprotocol/python-sdk/issues?q=state%3Aopen%20label%3A%22bug%22
needs confirmationNeeds confirmation that the PR is actually required or needed.https://github.com/modelcontextprotocol/python-sdk/issues?q=state%3Aopen%20label%3A%22needs%20confirmation%22
https://github.com/Muhammadtalha678
Muhammadtalha678https://github.com/Muhammadtalha678
on May 8, 2025https://github.com/modelcontextprotocol/python-sdk/issues/662#issue-3048033485
https://private-user-images.githubusercontent.com/79269235/441577703-50a48783-f3f3-499f-b69a-763c59161e3b.PNG?jwt=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3ODQxNTA1ODUsIm5iZiI6MTc4NDE1MDI4NSwicGF0aCI6Ii83OTI2OTIzNS80NDE1Nzc3MDMtNTBhNDg3ODMtZjNmMy00OTlmLWI2OWEtNzYzYzU5MTYxZTNiLlBORz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNjA3MTUlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjYwNzE1VDIxMTgwNVomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTI2NzE5MzkzMDRkZjNiNDAyZThkZWJjNWI2YjkxOGNhMTQyMmY2ZmNjNTQyMzg3MGU2YjUwYTcyZWQ5ODIzMmQmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JnJlc3BvbnNlLWNvbnRlbnQtdHlwZT1pbWFnZSUyRnBuZyJ9.4R5MmcEOBn1w2BVmDfEON7bu1TZwDNye1AXn3LIfp2s
http://127.0.0.1:3000/ssehttp://127.0.0.1:3000/sse
bugSomething isn't workinghttps://github.com/modelcontextprotocol/python-sdk/issues?q=state%3Aopen%20label%3A%22bug%22
needs confirmationNeeds confirmation that the PR is actually required or needed.https://github.com/modelcontextprotocol/python-sdk/issues?q=state%3Aopen%20label%3A%22needs%20confirmation%22
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.