| |
| """ |
| Sample MCP Server for ChatGPT Deep Research Integration |
| |
| This server implements the Model Context Protocol (MCP) with search and fetch |
| capabilities designed to work with ChatGPT's deep research feature. |
| """ |
|
|
| import logging |
| import os |
| import sys |
| from pathlib import Path |
| from typing import Dict, List, Any |
| sys.path.append(str(Path(__file__).parent / "src")) |
| from src.IberleyAI.Tools.IberleyIAPrompt import IberleyIAPrompt |
| from fastmcp import FastMCP |
|
|
| |
| logging.basicConfig(level=logging.INFO) |
| logger = logging.getLogger(__name__) |
|
|
| server_instructions = """ |
| This MCP server provides search and document retrieval capabilities |
| for deep research. Use the search tool to find relevant documents |
| based on keywords, then use the fetch tool to retrieve complete |
| document content with citations. |
| """ |
|
|
| def create_server(): |
| """Create and configure the MCP server with search and fetch tools.""" |
|
|
| mcp = FastMCP(name="IberleyIA MCP Server", |
| instructions=server_instructions) |
|
|
| @mcp.tool() |
| async def search(query: str) -> Dict[str, List[Dict[str, Any]]]: |
| """ |
| Search for legal information in the berleyAI chatbot connection |
| |
| This tool searches through the vector store to find semantically relevant matches. |
| Returns a list of search results with basic information. Use the fetch tool to get |
| complete document content. |
| |
| Args: |
| query: Search query string. Natural language queries work best for semantic search. |
| |
| Returns: |
| Dictionary with 'content' key containing list of matching documents. |
| Each result includes type, text. |
| """ |
| if not query or not query.strip(): |
| return {"content": []} |
|
|
| response = IberleyIAPrompt.iberley_ia_prompt(query) |
|
|
| result = { |
| "content": [ |
| { |
| "type": "text", |
| "text": response |
| } |
| ] |
| } |
| return result |
|
|
| @mcp.tool() |
| async def fetch(id: str) -> Dict[str, Any]: |
| """ |
| Search for legal information in the berleyAI chatbot connection given an id for a |
| legal document withing a query in natural language |
| |
| This tool searches through the vector store to find semantically relevant matches. |
| Returns a list of search results with basic information. Use the fetch tool to get |
| complete document content. |
| |
| Args: |
| query: Search query string. Natural language queries work best for semantic search. |
| |
| Returns: |
| Dictionary with 'content' key containing list of matching documents. |
| Each result includes type, text. |
| """ |
| if not id or not id.strip(): |
| return {"content": []} |
|
|
| response = IberleyIAPrompt.iberley_ia_prompt(id) |
|
|
| result = { |
| "content": [ |
| { |
| "type": "text", |
| "text": response |
| } |
| ] |
| } |
| return result |
|
|
| return mcp |
|
|
|
|
| def main(): |
| """Main function to start the MCP server.""" |
| |
| server = create_server() |
|
|
| logger.info("Starting MCP server on 0.0.0.0:7860") |
| logger.info("Server will be accessible via SSE transport") |
|
|
| try: |
| server.run(transport="sse", host="0.0.0.0", port=7860) |
| except KeyboardInterrupt: |
| logger.info("Server stopped by user") |
| except Exception as e: |
| logger.error(f"Server error: {e}") |
| raise |
|
|
|
|
| if __name__ == "__main__": |
| main() |
|
|