Why this low-level approach?
At this point, you might be thinking, Why would I want to do that? The previous approach was so easy! Well, there are a few reasons why you might want to use this approach. You may want to do the following:
- Use context managers to manage the lifecycle of your server: Here, you can do things such as connecting to a database or other services that are connected to your server. By having more control over the lifecycle of your server, you can ensure that your server is properly initialized and cleaned up when it’s no longer needed.
- Improve the architecture of your server: Having more control over how the server is built allows for more freedom in how tools and resources are registered, but also how incoming requests are handled. This increased control allows you to organize your code in a way that is more maintainable and scalable. This chapter shows you how you can organize your code with both a low-level server and a normal MCP server...