config.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 argparse
  16. import os
  17. from dataclasses import dataclass
  18. @dataclass(frozen=True)
  19. class Config:
  20. dsn: str
  21. transport: str
  22. host: str
  23. port: int
  24. pool_min_size: int
  25. pool_max_size: int
  26. log_level: str
  27. def load_config() -> Config:
  28. parser = argparse.ArgumentParser(description="Run Materialize MCP server")
  29. parser.add_argument(
  30. "--transport",
  31. choices=["stdio", "http", "sse"],
  32. default=os.getenv("MCP_TRANSPORT", "stdio"),
  33. help="Communication transport (default: stdio)",
  34. )
  35. parser.add_argument(
  36. "--mz-dsn",
  37. default=os.getenv(
  38. "MZ_DSN", "postgresql://materialize@localhost:6875/materialize"
  39. ),
  40. help="Materialize DSN (default: postgresql://materialize@localhost:6875/materialize)",
  41. )
  42. parser.add_argument(
  43. "--host",
  44. default=os.getenv("MCP_HOST", "0.0.0.0"),
  45. help="Server host (default: 0.0.0.0)",
  46. )
  47. parser.add_argument(
  48. "--port",
  49. type=int,
  50. default=os.getenv("MCP_PORT"),
  51. help="Server port (default: 3001 for SSE, 8001 for HTTP)",
  52. )
  53. parser.add_argument(
  54. "--pool-min-size",
  55. type=int,
  56. default=int(os.getenv("MCP_POOL_MIN_SIZE", "1")),
  57. help="Minimum connection pool size (default: 1)",
  58. )
  59. parser.add_argument(
  60. "--pool-max-size",
  61. type=int,
  62. default=int(os.getenv("MCP_POOL_MAX_SIZE", "10")),
  63. help="Maximum connection pool size (default: 10)",
  64. )
  65. parser.add_argument(
  66. "--log-level",
  67. choices=["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"],
  68. default=os.getenv("MCP_LOG_LEVEL", "INFO"),
  69. help="Logging level (default: INFO)",
  70. )
  71. args = parser.parse_args()
  72. return Config(
  73. dsn=args.mz_dsn,
  74. transport=args.transport,
  75. host=args.host,
  76. port=(
  77. int(args.port)
  78. if args.port is not None
  79. else 3001 if args.transport == "sse" else 8001
  80. ),
  81. pool_min_size=args.pool_min_size,
  82. pool_max_size=args.pool_max_size,
  83. log_level=args.log_level,
  84. )