|
| 1 | + |
| 2 | +# MCP Server for a tested AI Optimizer & Toolkit configuration |
| 3 | + |
| 4 | +**Version:** *Developer preview* |
| 5 | + |
| 6 | +## Introduction |
| 7 | +This document describe how to re-use the configuration tested in the **AI Optimizer & Toolkit** an expose it as an MCP tool to a local **Claude Desktop** and how to setup as a remote MCP server. This early draft implementation utilizes the `stdio` and `sse` to interact between the agent dashboard, represented by the **Claude Desktop**, and the tool. |
| 8 | + |
| 9 | +**NOTICE**: Only `Ollama` or `OpenAI` configurations are currently supported. Full support will come. |
| 10 | + |
| 11 | +## Pre-requisites. |
| 12 | +You need: |
| 13 | +- Node.js: v20.17.0+ |
| 14 | +- npx/npm: v11.2.0+ |
| 15 | +- uv: v0.7.10+ |
| 16 | +- Claude Desktop free |
| 17 | + |
| 18 | +## Setup |
| 19 | +With **[`uv`](https://docs.astral.sh/uv/getting-started/installation/)** installed, run the following commands in your current project directory `<PROJECT_DIR>/src/client/mcp/rag/`: |
| 20 | + |
| 21 | +```bash |
| 22 | +uv init --python=3.11 --no-workspace |
| 23 | +uv venv --python=3.11 |
| 24 | +source .venv/bin/activate |
| 25 | +uv add mcp langchain-core==0.3.52 oracledb~=3.1 langchain-community==0.3.21 langchain-huggingface==0.1.2 langchain-openai==0.3.13 langchain-ollama==0.3.2 |
| 26 | +``` |
| 27 | + |
| 28 | +## Export config |
| 29 | +In the **AI Optimizer & Toolkit** web interface, after tested a configuration, in `Settings/Client Settings`: |
| 30 | + |
| 31 | + |
| 32 | + |
| 33 | +* select the checkbox `Include Sensitive Settings` |
| 34 | +* press button `Download Settings` to download configuration in the project directory: `src/client/mcp/rag` as `optimizer_settings.json`. |
| 35 | +* in `<PROJECT_DIR>/src/client/mcp/rag/rag_base_optimizer_config_mcp.py` change filepath with the absolute path of your `optimizer_settings.json` file. |
| 36 | + |
| 37 | + |
| 38 | +## Standalone client |
| 39 | +There is a client that you can run without MCP via commandline to test it: |
| 40 | + |
| 41 | +```bash |
| 42 | +uv run rag_base_optimizer_config.py |
| 43 | +``` |
| 44 | + |
| 45 | +## Quick test via MCP "inspector" |
| 46 | + |
| 47 | +* Run the inspector: |
| 48 | + |
| 49 | +```bash |
| 50 | +npx @modelcontextprotocol/inspector uv run rag_base_optimizer_config_mcp.py |
| 51 | +``` |
| 52 | + |
| 53 | +* connect to the port `http://localhost:6274/` with your browser |
| 54 | +* setup the `Inspector Proxy Address` with `http://127.0.0.1:6277` |
| 55 | +* test the tool developed. |
| 56 | + |
| 57 | + |
| 58 | +## Claude Desktop setup |
| 59 | + |
| 60 | +* In **Claude Desktop** application, in `Settings/Developer/Edit Config`, get the `claude_desktop_config.json` to add the references to the local MCP server for RAG in the `<PROJECT_DIR>/src/client/mcp/rag/`: |
| 61 | +```json |
| 62 | +{ |
| 63 | + "mcpServers": { |
| 64 | + ... |
| 65 | + , |
| 66 | + "rag":{ |
| 67 | + "command":"bash", |
| 68 | + "args":[ |
| 69 | + "-c", |
| 70 | + "source <PROJECT_DIR>/src/client/mcp/rag/.venv/bin/activate && uv run <PROJECT_DIR>/src/client/mcp/rag/rag_base_optimizer_config_mcp.py" |
| 71 | + ] |
| 72 | + } |
| 73 | + } |
| 74 | +} |
| 75 | +``` |
| 76 | +* In **Claude Desktop** application, in `Settings/General/Claude Settings/Configure`, under `Profile` tab, update fields like: |
| 77 | +- `Full Name` |
| 78 | +- `What should we call you` |
| 79 | + |
| 80 | + and so on, putting in `What personal preferences should Claude consider in responses?` |
| 81 | + the following text: |
| 82 | + |
| 83 | +``` |
| 84 | +#INSTRUCTION: |
| 85 | +Always call the rag_tool tool when the user asks a factual or information-seeking question, even if you think you know the answer. |
| 86 | +Show the rag_tool message as-is, without modification. |
| 87 | +``` |
| 88 | +This will impose the usage of `rag_tool` in any case. |
| 89 | + |
| 90 | +**NOTICE**: If you prefer, in this agent dashboard or any other, you could setup a message in the conversation with the same content of `Instruction` to enforce the LLM to use the rag tool as well. |
| 91 | + |
| 92 | +* Restart **Claude Desktop**. |
| 93 | + |
| 94 | +* You will see two warnings on rag_tool configuration: they will disappear and will not cause any issue in activating the tool. |
| 95 | + |
| 96 | +* Start a conversation. You should see a pop up that ask to allow the `rag` tool usage to answer the questions: |
| 97 | + |
| 98 | + |
| 99 | + |
| 100 | + If the question is related to the knowledge base content stored in the vector store, you will have an answer based on that information. Otherwise, it will try to answer considering information on which has been trained the LLM o other tools configured in the same Claude Desktop. |
| 101 | + |
| 102 | + |
| 103 | +## Make a remote MCP server the RAG Tool |
| 104 | + |
| 105 | +In `rag_base_optimizer_config_mcp.py`: |
| 106 | + |
| 107 | +* Update the absolute path of your `optimizer_settings.json`. Example: |
| 108 | + |
| 109 | +```python |
| 110 | +rag.set_optimizer_settings_path("/Users/cdebari/Documents/GitHub/ai-optimizer-mcp-export/src/client/mcp/rag/optimizer_settings.json") |
| 111 | +``` |
| 112 | + |
| 113 | +* Substitute `Local` with `Remote client` line: |
| 114 | + |
| 115 | +```python |
| 116 | +#mcp = FastMCP("rag", port=8001) #Remote client |
| 117 | +mcp = FastMCP("rag") #Local |
| 118 | +``` |
| 119 | + |
| 120 | +* Substitute `stdio` with `sse` line of code: |
| 121 | +```python |
| 122 | +mcp.run(transport='stdio') |
| 123 | +#mcp.run(transport='sse') |
| 124 | +``` |
| 125 | + |
| 126 | +* Start MCP server in another shell with: |
| 127 | +```bash |
| 128 | +uv run rag_base_optimizer_config_mcp.py |
| 129 | +``` |
| 130 | + |
| 131 | + |
| 132 | +## Quick test |
| 133 | + |
| 134 | +* Run the inspector: |
| 135 | + |
| 136 | +```bash |
| 137 | +npx @modelcontextprotocol/inspector |
| 138 | +``` |
| 139 | + |
| 140 | +* connect the browser to `http://127.0.0.1:6274` |
| 141 | + |
| 142 | +* set the Transport Type to `SSE` |
| 143 | + |
| 144 | +* set the `URL` to `http://localhost:8001/sse` |
| 145 | + |
| 146 | +* test the tool developed. |
| 147 | + |
| 148 | + |
| 149 | + |
| 150 | +## Claude Desktop setup for remote/local server |
| 151 | +Claude Desktop, in free version, not allows to connect remote server. You can overcome, for testing purpose only, with a proxy library called `mcp-remote`. These are the options. |
| 152 | +If you have already installed Node.js v20.17.0+, it should work: |
| 153 | + |
| 154 | +* replace `rag` mcpServer, setting in `claude_desktop_config.json`: |
| 155 | +```json |
| 156 | +{ |
| 157 | + "mcpServers": { |
| 158 | + "remote": { |
| 159 | + "command": "npx", |
| 160 | + "args": [ |
| 161 | + "mcp-remote", |
| 162 | + "http://127.0.0.1:8001/sse" |
| 163 | + ] |
| 164 | + } |
| 165 | + } |
| 166 | +} |
| 167 | +``` |
| 168 | +* restart Claude Desktop. |
| 169 | + |
| 170 | +**NOTICE**: If you have any problem running, check the logs if it's related to an old npx/nodejs version used with mcp-remote library. Check with: |
| 171 | +```bash |
| 172 | +nvm -list |
| 173 | +``` |
| 174 | +if you have any other versions available than the default. It could happen that Claude Desktop uses the older one. Try to remove any other nvm versions available to force the use the only one avalable, at minimum v20.17.0+. |
| 175 | + |
| 176 | +* restart and test as remote server |
| 177 | + |
| 178 | + |
0 commit comments