Skip to content

Conversation

@abidlabs
Copy link
Member

@abidlabs abidlabs commented Jun 9, 2025

Previously, we were not injecting the correct starlette.Request object in a user's function, even if they decorated it with gr.Request (basically the latest user's network request information would be used for all users). Here, we fix that, taking advantage of this PR introduced in the latest version of mcp: modelcontextprotocol/python-sdk#816

You can test this by running an app like this:

import gradio as gr

def echo_headers(x, request: gr.Request):
    return str(dict(request.headers)["fake_username"])

gr.Interface(echo_headers, "textbox", "textbox").launch(mcp_server=True)

and then connect with two different clients, providing different values for the fake_username header:

image

@gradio-pr-bot
Copy link
Collaborator

gradio-pr-bot commented Jun 9, 2025

🪼 branch checks and previews

Name Status URL
Spaces ready! Spaces preview
Website ready! Website preview
🦄 Changes detected! Details

Install Gradio from this PR

pip install https://gradio-pypi-previews.s3.amazonaws.com/b176db9d6e1bffd36f5b2ecd54b425e4cec3dab8/gradio-5.33.0-py3-none-any.whl

Install Gradio Python Client from this PR

pip install "gradio-client @ git+https://github.com/gradio-app/gradio@b176db9d6e1bffd36f5b2ecd54b425e4cec3dab8#subdirectory=client/python"

Install Gradio JS Client from this PR

npm install https://gradio-npm-previews.s3.amazonaws.com/b176db9d6e1bffd36f5b2ecd54b425e4cec3dab8/gradio-client-1.15.1.tgz

Use Lite from this PR

<script type="module" src="https://gradio-lite-previews.s3.amazonaws.com/b176db9d6e1bffd36f5b2ecd54b425e4cec3dab8/dist/lite.js""></script>

@gradio-pr-bot
Copy link
Collaborator

gradio-pr-bot commented Jun 9, 2025

🦄 change detected

This Pull Request includes changes to the following packages.

Package Version
gradio patch
  • Maintainers can select this checkbox to manually select packages to update.

With the following changelog entry.

Inject the Request information in MCP tool calls

Maintainers or the PR author can modify the PR title to modify this entry.

Something isn't right?

  • Maintainers can change the version label to modify the version bump.
  • If the bot has failed to detect any changes, or if this pull request needs to update multiple packages to different versions or requires a more comprehensive changelog entry, maintainers can update the changelog file directly.

@abidlabs abidlabs added the v: patch A change that requires a patch release label Jun 9, 2025
@abidlabs abidlabs requested a review from freddyaboulton June 9, 2025 23:07
@abidlabs abidlabs marked this pull request as ready for review June 9, 2025 23:07
@abidlabs abidlabs requested a review from aliabid94 June 9, 2025 23:07
@abidlabs abidlabs changed the title Provide correct context requests information in mcp tool call Inject the Request in MCP tool calls Jun 9, 2025
@abidlabs abidlabs changed the title Inject the Request in MCP tool calls Inject the Request information in MCP tool calls Jun 9, 2025
@abidlabs abidlabs requested review from hysts and yvrjsharma June 9, 2025 23:16
Copy link
Collaborator

@freddyaboulton freddyaboulton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mainly just one question

gradio/mcp.py Outdated
arguments: The arguments to pass to the tool.
"""
context_request = self.mcp_server.request_context.request
assert context_request is not None # noqa: S101
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rather than noqa comment, raise RuntimeError if request_context is None

gradio/mcp.py Outdated
route_path="/gradio_api/mcp/messages",
root_path=self.root_path,
)
print("root_url", root_url)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove print

gradio/mcp.py Outdated
assert context_request is not None # noqa: S101
root_url = route_utils.get_root_url(
request=context_request,
route_path="/gradio_api/mcp/messages",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If using the http protocol, the path /gradio_api/mcp/http, no? is this going to cause problems?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch I'll fix this

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be fixed now

Copy link
Collaborator

@freddyaboulton freddyaboulton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clean @abidlabs !

@abidlabs abidlabs merged commit 1eb4e2e into main Jun 10, 2025
22 checks passed
@abidlabs abidlabs deleted the mcp-request branch June 10, 2025 00:13
@abidlabs
Copy link
Member Author

Thanks so much @freddyaboulton!

Oncorporation pushed a commit to Oncorporation/gradio that referenced this pull request Jun 17, 2025
* changes

* add changeset

* changes

* add changeset

* changes

* simplify

* pin mcp version

* changes

* changes

* undo

* add changeset

* changes

* changes

* format

---------

Co-authored-by: gradio-pr-bot <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

v: patch A change that requires a patch release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants