Skip to content

Add Streamable HTTP Support and Graceful Shutdown for Postgres MCP Server #78

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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

ahmedmustahid
Copy link

Summary

This PR implements streamable HTTP transport support and fixes graceful shutdown handling for the Postgres MCP server, addressing issues with signal handling in stdio transport environments.

Changes Made

Core Improvements

  • Streamable HTTP Transport: Added support for HTTP-based communication alongside existing stdio transport
  • Graceful Shutdown: Implemented proper signal handling that works reliably with both stdio and HTTP transports

Modified Files

  • src/postgres_mcp/__init__.py: Simplified main entry point to properly delegate to server.main()
  • src/postgres_mcp/server.py: Enhanced with streamable HTTP support and robust shutdown handling

Key Features

  • Multi-transport Support: Server now supports all of stdio, sse and streamable HTTP
  • Signal Handling: Proper SIGINT/SIGTERM handling that actually terminates the process
  • Database Cleanup: Ensures database connections are properly closed during shutdown

Problem Solved

Previously,

  • there was no streamable http support
  • pressing Ctrl+C would trigger "asyncio.run() cannot be called from a running event loop" error

Solution

  • Implemented threading-based signal handling that works with blocked event loops
  • Added streamable HTTP transport as an alternative to stdio
  • Simplified shutdown logic with proper resource cleanup
  • Used appropriate exit mechanisms for different transport types

Testing

  • ✅ Ctrl+C properly terminates the server
  • ✅ Database connections are closed cleanly
  • ✅ Works with both stdio and HTTP transports

Backward Compatibility

  • All existing MCP client integrations continue to work unchanged
  • Stdio transport remains the default for MCP usage
  • HTTP transport is available as an optional feature

Usage Examples

Default MCP Usage (stdio)

uv run postgres-mcp  # Uses stdio transport as before

Streamable HTTP Usage

# uv run postgres-mcp --transport sse  #sse
uv run postgres-mcp --transport streamable_http # Enables HTTP transport

Notes

  • Fixes the "asyncio.run() cannot be called from a running event loop" error
  • Resolves process hanging issues during shutdown
  • Maintains full compatibility with existing MCP client implementations
  • Adds foundation for future web-based management interfaces

Type: Enhancement, Bug Fix
Breaking Changes: None
Requires Migration: No

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.

1 participant