@@ -17,9 +17,11 @@ A Python utility package for building Model Context Protocol (MCP) servers.
1717 - [ Optional Dependencies] ( #optional-dependencies )
1818 - [ Usage] ( #usage )
1919 - [ Basic MCP Server] ( #basic-mcp-server )
20- - [ Flask with Redis Example] ( #flask-with-redis -example )
20+ - [ Flask Example] ( #flask-example )
2121 - [ SQLAlchemy Transaction Handling Example] ( #sqlalchemy-transaction-handling-example )
22+ - [ Running with Gunicorn] ( #running-with-gunicorn )
2223 - [ Connecting with MCP Clients] ( #connecting-with-mcp-clients )
24+ - [ Cursor] ( #cursor )
2325 - [ Claude Desktop] ( #claude-desktop )
2426 - [ Installing via Smithery] ( #installing-via-smithery )
2527 - [ Installing via PyPI] ( #installing-via-pypi )
@@ -94,40 +96,27 @@ def get_weather(city: str) -> str:
9496 return " sunny"
9597```
9698
97- ### Flask with Redis Example
99+ ### Flask Example
100+
101+ For production use, you can use a simple Flask app with the mcp server and
102+ support [ Streamable HTTP] ( https://modelcontextprotocol.io/specification/2025-06-18/basic/transports#streamable-http )
103+ from version 2025-06-18.
98104
99- For production use, you can integrate the MCP server with Flask and Redis for better message handling:
100105
101106``` python
102107from flask import Flask, Response, url_for, request
103- import redis
104- from mcp_utils.queue import RedisResponseQueue
105-
106- # Setup Redis client
107- redis_client = redis.Redis(host = " localhost" , port = 6379 , db = 0 )
108108
109109# Create Flask app and MCP server with Redis queue
110110app = Flask(__name__ )
111111mcp = MCPServer(
112112 " example" ,
113113 " 1.0" ,
114- response_queue = RedisResponseQueue(redis_client)
115114)
116115
117- @app.route (" /sse" )
118- def sse ():
119- session_id = mcp.generate_session_id()
120- messages_endpoint = url_for(" message" , session_id = session_id)
121- return Response(
122- mcp.sse_stream(session_id, messages_endpoint),
123- mimetype = " text/event-stream"
124- )
125-
126-
127- @app.route (" /message/<session_id>" , methods = [" POST" ])
128- def message (session_id ):
129- mcp.handle_message(session_id, request.get_json())
130- return " " , 202
116+ @app.route (" /mcp" , methods = [" POST" ])
117+ def mcp_route ():
118+ response = mcp.handle_message(request.get_json())
119+ return jsonify(response.model_dump(exclude_none = True ))
131120
132121
133122if __name__ == " __main__" :
@@ -142,11 +131,6 @@ For production use, you can integrate the MCP server with Flask, Redis, and SQLA
142131from flask import Flask, request
143132from sqlalchemy.orm import Session
144133from sqlalchemy import create_engine
145- import redis
146- from mcp_utils.queue import RedisResponseQueue
147-
148- # Setup Redis client
149- redis_client = redis.Redis(host = " localhost" , port = 6379 , db = 0 )
150134
151135# Create engine for PostgreSQL database
152136engine = create_engine(" postgresql://user:pass@localhost/dbname" )
@@ -156,31 +140,86 @@ app = Flask(__name__)
156140mcp = MCPServer(
157141 " example" ,
158142 " 1.0" ,
159- response_queue = RedisResponseQueue(redis_client)
160143)
161144
162- @app.route (" /message/<session_id> " , methods = [" POST" ])
163- def message ( session_id ):
145+ @app.route (" /mcp " , methods = [" POST" ])
146+ def mcp_route ( ):
164147 with Session(engine) as session:
165148 try :
166- mcp.handle_message(session_id, request.get_json())
149+ response = mcp.handle_message(request.get_json())
167150 session.commit()
168- return " " , 202
169- except Exception as e:
151+ except :
170152 session.rollback()
171153 raise
154+ else :
155+ return jsonify(response.model_dump(exclude_none = True ))
156+
172157
173158if __name__ == " __main__" :
174159 app.run(debug = True )
175160```
176161
177162For 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.
178163
164+ ### Running with Gunicorn
165+
166+ Gunicorn is a better approach to running even locally. To run the app with gunicorn
167+
168+ ``` python
169+ from gunicorn.app.base import BaseApplication
170+
171+ class FlaskApplication (BaseApplication ):
172+ def __init__ (self , app , options = None ):
173+ self .options = options or {}
174+ self .application = app
175+ super ().__init__ ()
176+
177+ def load_config (self ):
178+ config = {
179+ key: value
180+ for key, value in self .options.items()
181+ if key in self .cfg.settings
182+ }
183+ for key, value in config.items():
184+ self .cfg.set(key.lower(), value)
185+
186+ def load (self ):
187+ return self .application
188+
189+
190+ if __name__ == " __main__" :
191+ handler = logging.StreamHandler(sys.stdout)
192+ formatter = logging.Formatter(" [%(asctime)s ] [%(levelname)s ] %(name)s : %(message)s " )
193+ handler.setFormatter(formatter)
194+ logger.addHandler(handler)
195+ options = {
196+ " bind" : " 0.0.0.0:9000" ,
197+ " workers" : 1 ,
198+ " worker_class" : " gevent" ,
199+ " loglevel" : " debug" ,
200+ }
201+ FlaskApplication(app, options).run()
202+ ```
203+
179204## Connecting with MCP Clients
180205
206+ ### Cursor
207+
208+ * Edit MCP settings and add to configuration
209+
210+ ``` json
211+ {
212+ "mcpServers" : {
213+ "server-name" : {
214+ "url" : " http://localhost:9000/mcp"
215+ }
216+ }
217+ }
218+ ```
219+
181220### Claude Desktop
182221
183- Currently, only Claude Desktop (not claude.ai) can connect to MCP servers. As of this writing, Claude Desktop does not support MCP through SSE and only supports stdio. To connect Claude Desktop with an MCP server, you'll need to use [ mcp-proxy] ( https://github.com/sparfenyuk/mcp-proxy ) .
222+ As of this writing, Claude Desktop does not support MCP through SSE and only supports stdio. To connect Claude Desktop with an MCP server, you'll need to use [ mcp-proxy] ( https://github.com/sparfenyuk/mcp-proxy ) .
184223
185224Configuration example for Claude Desktop:
186225
0 commit comments