Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use httpx.AsyncClient for async http requests #34

Merged
merged 2 commits into from
Nov 25, 2024

Conversation

graingert
Copy link
Contributor

@graingert graingert commented Nov 25, 2024

requests.Session does blocking IO which blocks the event loop! No other requests can be processed concurrently

@@ -768,7 +790,7 @@ uvicorn.run(app, host="0.0.0.0", port=8000)
</Step>
<Step title="Run tests">
```bash
uv add --dev pytest pytest-asyncio
Copy link
Contributor Author

Choose a reason for hiding this comment

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

httpx and starlette both depend on anyio which ships with an async test framework - so you don't need to install an extra one here

Copy link
Member

@jspahrsummers jspahrsummers left a comment

Choose a reason for hiding this comment

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

The change to httpx looks great—thank you!

Re: anyio, although we use it in our SDK, it may make examples or getting started a bit more confusing than using asyncio, because the latter is part of the standard library. We've purposely opted to stick with asyncio for now. Do you mind reverting those bits? 🙏

@@ -108,20 +108,20 @@ Let's build your first MCP server in Python! We'll create a weather server that
Add this functionality:

```python
# Create reusable session
http = requests.Session()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

There's nowhere to run a context manager and expose it to request handlers, so we're forced to create a new client each request: modelcontextprotocol/python-sdk#67

@graingert
Copy link
Contributor Author

Happy to drop the use of anyio.run, but having multiple async plugins for pytest installed is overkill

@dsp-ant
Copy link
Member

dsp-ant commented Nov 25, 2024

Happy to drop the use of anyio.run, but having multiple async plugins for pytest installed is overkill

To stay consistent across the example, the implementation should use one async runtime for now. I would think it's best if for now we keep asyncio and create an issue to follow up if we should just switch the whole example, including the implementation itself, to anyio given that it is transient a dependency anyway.

@graingert
Copy link
Contributor Author

Happy to drop the use of anyio.run, but having multiple async plugins for pytest installed is overkill

To stay consistent across the example, the implementation should use one async runtime for now. I would think it's best if for now we keep asyncio and create an issue to follow up if we should just switch the whole example, including the implementation itself, to anyio given that it is transient a dependency anyway.

Yes I configured it to use asyncio only

Copy link
Member

@jspahrsummers jspahrsummers left a comment

Choose a reason for hiding this comment

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

Thank you!

@jspahrsummers jspahrsummers merged commit 1df6f8b into modelcontextprotocol:main Nov 25, 2024
@graingert graingert deleted the patch-1 branch November 25, 2024 21:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants