transports.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # Copyright Materialize, Inc. and contributors. All rights reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License in the LICENSE file at the
  6. # root of this repository, or online at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import contextlib
  16. from collections.abc import AsyncIterator
  17. from mcp.server import InitializationOptions, Server
  18. from mcp.server.streamable_http_manager import StreamableHTTPSessionManager
  19. from mcp_materialize.config import Config
  20. async def stdio_transport(server: Server, options: InitializationOptions):
  21. from mcp import stdio_server
  22. async with stdio_server() as (read_stream, write_stream):
  23. await server.run(
  24. read_stream,
  25. write_stream,
  26. options,
  27. )
  28. async def sse_transport(server: Server, options: InitializationOptions, cfg: Config):
  29. import uvicorn
  30. from mcp.server.sse import SseServerTransport
  31. from starlette.applications import Starlette
  32. from starlette.routing import Mount, Route
  33. from starlette.types import Receive, Scope, Send
  34. sse = SseServerTransport("/messages/")
  35. async def handle_sse(scope: Scope, receive: Receive, send: Send):
  36. async with sse.connect_sse(scope, receive, send) as (
  37. read_stream,
  38. write_stream,
  39. ):
  40. await server.run(read_stream, write_stream, options)
  41. starlette_app = Starlette(
  42. routes=[
  43. Route("/sse", endpoint=handle_sse, methods=["GET"]),
  44. Mount("/messages/", app=sse.handle_post_message),
  45. ],
  46. )
  47. uv_server = uvicorn.Server(
  48. uvicorn.Config(
  49. starlette_app,
  50. host=cfg.host,
  51. port=cfg.port,
  52. log_level=cfg.log_level.lower(),
  53. )
  54. )
  55. await uv_server.serve()
  56. async def http_transport(server: Server, cfg: Config):
  57. import uvicorn
  58. from starlette.applications import Starlette
  59. from starlette.routing import Mount
  60. from starlette.types import Receive, Scope, Send
  61. session_manager = StreamableHTTPSessionManager(
  62. app=server,
  63. stateless=True,
  64. )
  65. async def handle_http(scope: Scope, receive: Receive, send: Send):
  66. await session_manager.handle_request(scope, receive, send)
  67. @contextlib.asynccontextmanager
  68. async def lifespan(app: Starlette) -> AsyncIterator[None]:
  69. async with session_manager.run():
  70. yield
  71. starlette_app = Starlette(
  72. routes=[
  73. Mount("/mcp", app=handle_http),
  74. ],
  75. lifespan=lifespan,
  76. )
  77. uv_server = uvicorn.Server(
  78. uvicorn.Config(
  79. starlette_app,
  80. host=cfg.host,
  81. port=cfg.port,
  82. log_level=cfg.log_level.lower(),
  83. )
  84. )
  85. await uv_server.serve()