Skip to content

Commit d93498d

Browse files
committed
fix: resolve database connection ssl issue
1 parent 1afa260 commit d93498d

2 files changed

Lines changed: 27 additions & 24 deletions

File tree

.env.example

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,14 @@ DATABASE_URL=postgresql+asyncpg://postgres:postgres@localhost:5432/mcp_server
1919
# Pagination limits for list/search tools
2020
DEFAULT_PAGE_SIZE=10
2121
MAX_PAGE_SIZE=50
22+
23+
# CREATE USER mcp_reader WITH PASSWORD 'mypassword';
24+
# CREATE USER mcp_writer WITH PASSWORD 'mypassword';
25+
26+
# CREATE DATABASE mcp_db;
27+
28+
# GRANT ALL PRIVILEGES ON DATABASE mcp_db TO mcp_writer;
29+
# GRANT USAGE ON SCHEMA public TO mcp_reader;
30+
31+
# ALTER DEFAULT PRIVILEGES IN SCHEMA public
32+
# GRANT SELECT ON TABLES TO mcp_reader;

src/core/config.py

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,25 @@ def normalize_database_url_for_async(url: str) -> str:
1212
if u.startswith("postgresql://") and not u.startswith("postgresql+"):
1313
u = "postgresql+asyncpg://" + u[len("postgresql://") :]
1414

15-
# asyncpg does not accept sslmode=..., convert psycopg-style URLs.
15+
# SQLAlchemy's asyncpg dialect forwards URL query params as kwargs to
16+
# asyncpg's connect(). asyncpg.connect() accepts `ssl` but not `sslmode`,
17+
# so rename sslmode→ssl while keeping the value as a valid SSLMode string
18+
# (disable, allow, prefer, require, verify-ca, verify-full).
1619
if u.startswith("postgresql+asyncpg://"):
1720
parsed = urlsplit(u)
1821
query = parse_qsl(parsed.query, keep_blank_values=True)
19-
has_ssl = any(key == "ssl" for key, _ in query)
20-
if not has_ssl:
21-
normalized_query: list[tuple[str, str]] = []
22-
for key, value in query:
23-
if key != "sslmode":
24-
normalized_query.append((key, value))
25-
continue
26-
27-
mode = value.strip().lower()
28-
if mode == "disable":
29-
normalized_query.append(("ssl", "false"))
30-
else:
31-
normalized_query.append(("ssl", "true"))
32-
33-
u = urlunsplit(
34-
(
35-
parsed.scheme,
36-
parsed.netloc,
37-
parsed.path,
38-
urlencode(normalized_query, doseq=True),
39-
parsed.fragment,
40-
)
41-
)
22+
if any(key == "sslmode" for key, _ in query):
23+
query = [
24+
("ssl", value) if key == "sslmode" else (key, value)
25+
for key, value in query
26+
]
27+
u = urlunsplit((
28+
parsed.scheme,
29+
parsed.netloc,
30+
parsed.path,
31+
urlencode(query, doseq=True),
32+
parsed.fragment,
33+
))
4234
return u
4335

4436

0 commit comments

Comments
 (0)