Sqlserver MCP Test
Testing experience using OPENAI api and creating an mcp for sql server to search for information within the database so it can be queried using natural language
Ask AI about Sqlserver MCP Test
Powered by Claude Β· Grounded in docs
I know everything about Sqlserver MCP Test. Ask me about installation, configuration, usage, or troubleshooting.
0/500
Reviews
Documentation
MCP SQL Server Chat - Refactored Architecture
This project has been refactored from a monolithic script into a modular, maintainable multi-file structure following Python best practices.
File Structure
βββ config.py # Configuration and constants
βββ mcp_integration.py # MCP protocol utilities and tool conversion
βββ openai_integration.py # OpenAI client utilities and response handling
βββ chat.py # Core chat orchestration logic
βββ chat_session.py # Session state and message history management
βββ streamlit_app.py # Main Streamlit UI application
βββ README.md # This file
Module Descriptions
config.py
Centralized configuration and constants:
MODEL: OpenAI model to useMCP_SERVER_COMMAND,MCP_SERVER_SCRIPT: MCP server configuration- UI settings (page title, icon, layout)
- System prompt and default messages
Best Practice: Keeps all magic strings and configuration in one place for easy maintenance.
mcp_integration.py
MCP (Model Context Protocol) utilities:
mcp_tool_to_openai_function(): Converts MCP tools to OpenAI function formatcreate_server_params(): Creates MCP server configurationget_mcp_tools(): Fetches and converts available tools
Best Practice: Separates MCP-specific logic from business logic.
openai_integration.py
OpenAI client and response handling:
initialize_openai_client(): Sets up OpenAI client with API key validationextract_function_calls(): Parses function calls from OpenAI responsesformat_tool_output(): Formats tool execution results
Best Practice: Encapsulates OpenAI-specific operations in a dedicated module.
chat.py
Core chat turn orchestration:
run_chat_turn(): Main async function coordinating MCP + OpenAI interaction- Handles tool discovery, function calling, and result feeding
Best Practice: Clean separation of business logic from UI and client initialization.
chat_session.py
Session state management:
initialize_session_state(): Sets up Streamlit session stateget_messages(),add_user_message(),add_assistant_message(): Session accessorsget_display_messages(): Filters out system messages for display
Best Practice: Centralized state management for easier testing and maintenance.
streamlit_app.py
Main Streamlit UI application:
configure_page(): Page configurationrender_sidebar(): Settings panelrender_message_history(): Chat displayhandle_user_input(): User input processingmain(): Application entry point
Best Practice: UI logic is clean and focused on presentation.
Running the Application
streamlit run streamlit_app.py
Environment Setup
# Set your OpenAI API key
export OPENAI_API_KEY="your-api-key-here"
# Ensure mcp_mssql_server.py is in the same directory or update config.py
Key Benefits of This Refactoring
- Modularity: Each module has a single responsibility
- Testability: Isolated functions can be unit tested independently
- Reusability: Modules can be imported and used in other projects
- Maintainability: Changes to one concern don't affect others
- Scalability: Easy to add new features without cluttering the codebase
- Documentation: Docstrings and clear code organization
- Configuration Management: All settings in one place
- Error Handling: Better separation for targeted error management
- Async Management: Proper asyncio handling with fallbacks
Extending the Architecture
Adding a New MCP Tool Handler
- Add utility function to
mcp_integration.py - Import and use in
chat.py
Adding UI Features
- Create new functions in
streamlit_app.pyor a newui_components.py - Import utilities as needed
Adding New Configuration
- Add constants to
config.py - Import in relevant modules
Type Hints
All modules use Python type hints for better IDE support and code clarity.
Error Handling
openai_integration.pyvalidates API key on client initializationstreamlit_app.pyhandles async event loop edge caseschat.pycoordinates error handling across MCP and OpenAI
Testing Recommendations
The modular structure makes it easy to test individual components:
# Example: test mcp_integration.py
from mcp_integration import mcp_tool_to_openai_function
# Example: test openai_integration.py
from openai_integration import extract_function_calls
Each function is isolated and can be tested independently with mock objects.
