Skip to content

Commit 4e72ebf

Browse files
committed
Add sqlalchemy example
1 parent d03b26b commit 4e72ebf

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ A Python utility package for building Model Context Protocol (MCP) servers.
1818
- [Usage](#usage)
1919
- [Basic MCP Server](#basic-mcp-server)
2020
- [Flask with Redis Example](#flask-with-redis-example)
21+
- [SQLAlchemy Transaction Handling Example](#sqlalchemy-transaction-handling-example)
2122
- [Connecting with MCP Clients](#connecting-with-mcp-clients)
2223
- [Claude Desktop](#claude-desktop)
2324
- [Installing via Smithery](#installing-via-smithery)
@@ -130,6 +131,46 @@ if __name__ == "__main__":
130131
app.run(debug=True)
131132
```
132133

134+
### SQLAlchemy Transaction Handling Example
135+
136+
For production use, you can integrate the MCP server with Flask, Redis, and SQLAlchemy for better message handling and database transaction management:
137+
138+
```python
139+
from flask import Flask, request
140+
from sqlalchemy.orm import Session
141+
from sqlalchemy import create_engine
142+
import redis
143+
from mcp_utils.queue import RedisResponseQueue
144+
145+
# Setup Redis client
146+
redis_client = redis.Redis(host="localhost", port=6379, db=0)
147+
148+
# Create engine for PostgreSQL database
149+
engine = create_engine("postgresql://user:pass@localhost/dbname")
150+
151+
# Create Flask app and MCP server with Redis queue
152+
app = Flask(__name__)
153+
mcp = MCPServer(
154+
"example",
155+
"1.0",
156+
response_queue=RedisResponseQueue(redis_client)
157+
)
158+
159+
@app.route("/message/<session_id>", methods=["POST"])
160+
def message(session_id):
161+
with Session(engine) as session:
162+
try:
163+
mcp.handle_message(session_id, request.get_json())
164+
session.commit()
165+
return "", 202
166+
except Exception as e:
167+
session.rollback()
168+
raise
169+
170+
if __name__ == "__main__":
171+
app.run(debug=True)
172+
```
173+
133174
For a more comprehensive example including logging setup and session management, check out the [example Flask application](https://github.com/fulfilio/mcp-utils/blob/main/examples/flask_app.py) in the repository.
134175

135176
## Connecting with MCP Clients

0 commit comments

Comments
 (0)