|
| 1 | +--- |
| 2 | +layout: default |
| 3 | +title: MCP Streamable HTTP Server |
| 4 | +parent: MCP server APIs |
| 5 | +grand_parent: ML Commons APIs |
| 6 | +nav_order: 50 |
| 7 | +--- |
| 8 | + |
| 9 | +# MCP Streamable HTTP Server |
| 10 | +**Introduced 3.3** |
| 11 | +{: .label .label-purple } |
| 12 | + |
| 13 | +The MCP server is exposed via the `/_plugins/_ml/mcp` endpoint and implements the Streamable HTTP transport defined by the Model Context Protocol (MCP). It allows agents/clients to connect to OpenSearch and discover and call available tools. |
| 14 | + |
| 15 | +This server does not open a persistent SSE connection with the client; all communication happens over stateless HTTP calls. |
| 16 | +If a client sends a GET request (typically to establish an SSE connection), the server returns a 405 Method Not Allowed response, allowing the client to continue with POST-based communication. |
| 17 | + |
| 18 | + |
| 19 | +To learn more about the transport, see the [official MCP documentation](https://modelcontextprotocol.io/specification/2025-03-26/basic/transports) |
| 20 | +{: .note } |
| 21 | + |
| 22 | +## Prerequisites |
| 23 | +- Enable the MCP server setting. |
| 24 | +- Optionally register tools so you can see and call them via the MCP server. |
| 25 | + |
| 26 | +### Enable the MCP server |
| 27 | +```json |
| 28 | +PUT /_cluster/settings |
| 29 | +{ |
| 30 | + "persistent": { |
| 31 | + "plugins.ml_commons.mcp_server_enabled": "true" |
| 32 | + } |
| 33 | +} |
| 34 | +``` |
| 35 | +{% include copy-curl.html %} |
| 36 | + |
| 37 | +### (Optional) Register tools |
| 38 | +Register tools so clients can discover and call them. See [Register MCP tools]({{site.url}}{{site.baseurl}}/ml-commons-plugin/api/mcp-server-apis/register-mcp-tools/). |
| 39 | + |
| 40 | +## Usage |
| 41 | +You can connect to the MCP server using any client that supports the Streamable HTTP transport. Below are two approaches: |
| 42 | + |
| 43 | +### Using an MCP client |
| 44 | +The following example uses `fastmcp` to initialize a connection, list tools, and call a tool: |
| 45 | + |
| 46 | +```python |
| 47 | +import asyncio, logging |
| 48 | +from fastmcp import Client |
| 49 | + |
| 50 | + |
| 51 | +async def main(): |
| 52 | + async with Client("http://localhost:9200/_plugins/_ml/mcp") as client: |
| 53 | + for t in await client.list_tools(): |
| 54 | + print(t.name) |
| 55 | + r = await client.call_tool("ListIndexTool", {}) |
| 56 | + print("result: ", r) |
| 57 | + |
| 58 | +asyncio.run(main()) |
| 59 | +``` |
| 60 | + |
| 61 | +### Manual invocation (for debugging) |
| 62 | +While not required for normal usage, you can manually invoke the MCP server using JSON-RPC calls over HTTP. The sequence below mirrors typical MCP client behavior. |
| 63 | + |
| 64 | +#### 1. Initialize connection |
| 65 | +Example request: |
| 66 | + |
| 67 | +```json |
| 68 | +POST /_plugins/_ml/mcp |
| 69 | +{ |
| 70 | + "jsonrpc": "2.0", |
| 71 | + "id": 1, |
| 72 | + "method": "initialize", |
| 73 | + "params": { |
| 74 | + "protocolVersion": "2025-03-26", |
| 75 | + "capabilities": { |
| 76 | + "roots": { |
| 77 | + "listChanged": true |
| 78 | + }, |
| 79 | + "sampling": {} |
| 80 | + }, |
| 81 | + "clientInfo": { |
| 82 | + "name": "test-client", |
| 83 | + "version": "1.0.0" |
| 84 | + } |
| 85 | + } |
| 86 | +} |
| 87 | +``` |
| 88 | +{% include copy-curl.html %} |
| 89 | + |
| 90 | +Example response: |
| 91 | + |
| 92 | +```json |
| 93 | +200 OK |
| 94 | +{ |
| 95 | + "jsonrpc": "2.0", |
| 96 | + "id": 1, |
| 97 | + "result": { |
| 98 | + "protocolVersion": "2025-03-26", |
| 99 | + "capabilities": { |
| 100 | + "logging": {}, |
| 101 | + "prompts": { |
| 102 | + "listChanged": false |
| 103 | + }, |
| 104 | + "resources": { |
| 105 | + "subscribe": false, |
| 106 | + "listChanged": false |
| 107 | + }, |
| 108 | + "tools": { |
| 109 | + "listChanged": true |
| 110 | + } |
| 111 | + }, |
| 112 | + "serverInfo": { |
| 113 | + "name": "OpenSearch-MCP-Stateless-Server", |
| 114 | + "version": "0.1.0" |
| 115 | + }, |
| 116 | + "instructions": "OpenSearch MCP Stateless Server - provides access to ML tools without sessions" |
| 117 | + } |
| 118 | +} |
| 119 | +``` |
| 120 | + |
| 121 | +#### 2. Send initialization complete notification |
| 122 | +Example request: |
| 123 | + |
| 124 | +```json |
| 125 | +POST /_plugins/_ml/mcp |
| 126 | +{ |
| 127 | + "jsonrpc": "2.0", |
| 128 | + "method": "notifications/initialized", |
| 129 | + "params": {} |
| 130 | +} |
| 131 | +``` |
| 132 | +{% include copy-curl.html %} |
| 133 | + |
| 134 | +Example response: |
| 135 | + |
| 136 | +```json |
| 137 | +202 Accepted |
| 138 | +``` |
| 139 | + |
| 140 | +#### 3. List available tools |
| 141 | +Use this to discover tools. See also [List MCP tools]({{site.url}}{{site.baseurl}}/ml-commons-plugin/api/mcp-server-apis/list-mcp-tools/). |
| 142 | + |
| 143 | +Example request: |
| 144 | + |
| 145 | +```json |
| 146 | +POST /_plugins/_ml/mcp |
| 147 | +{ |
| 148 | + "jsonrpc": "2.0", |
| 149 | + "id": 2, |
| 150 | + "method": "tools/list", |
| 151 | + "params": {} |
| 152 | +} |
| 153 | +``` |
| 154 | +{% include copy-curl.html %} |
| 155 | + |
| 156 | +Example response: |
| 157 | + |
| 158 | +```json |
| 159 | +200 OK |
| 160 | +{ |
| 161 | + "jsonrpc": "2.0", |
| 162 | + "id": 2, |
| 163 | + "result": { |
| 164 | + "tools": [ |
| 165 | + { |
| 166 | + "name": "ListIndexTool", |
| 167 | + "description": "This tool returns information about indices in the OpenSearch cluster along with the index `health`, `status`, `index`, `uuid`, `pri`, `rep`, `docs.count`, `docs.deleted`, `store.size`, `pri.store. size `, `pri.store.size`, `pri.store`. Optional arguments: 1. `indices`, a comma-delimited list of one or more indices to get information from (default is an empty list meaning all indices). Use only valid index names. 2. `local`, whether to return information from the local node only instead of the cluster manager node (Default is false)", |
| 168 | + "inputSchema": { |
| 169 | + "type": "object", |
| 170 | + "properties": { |
| 171 | + "indices": { |
| 172 | + "type": "array", |
| 173 | + "items": { |
| 174 | + "type": "string" |
| 175 | + }, |
| 176 | + "description": "OpenSearch index name list, separated by comma. for example: [\"index1\", \"index2\"], use empty array [] to list all indices in the cluster" |
| 177 | + } |
| 178 | + }, |
| 179 | + "additionalProperties": false |
| 180 | + } |
| 181 | + } |
| 182 | + ] |
| 183 | + } |
| 184 | +} |
| 185 | +``` |
| 186 | + |
| 187 | +#### 4. Call a tool |
| 188 | +Example request: |
| 189 | + |
| 190 | +```json |
| 191 | +POST /_plugins/_ml/mcp |
| 192 | +{ |
| 193 | + "jsonrpc": "2.0", |
| 194 | + "id": 3, |
| 195 | + "method": "tools/call", |
| 196 | + "params": { |
| 197 | + "name": "ListIndexTool", |
| 198 | + "arguments": { |
| 199 | + "indices": [] |
| 200 | + } |
| 201 | + } |
| 202 | +} |
| 203 | +``` |
| 204 | +{% include copy-curl.html %} |
| 205 | + |
| 206 | +Example response: |
| 207 | + |
| 208 | +```json |
| 209 | +200 OK |
| 210 | +{ |
| 211 | + "jsonrpc": "2.0", |
| 212 | + "id": 3, |
| 213 | + "result": { |
| 214 | + "content": [ |
| 215 | + { |
| 216 | + "type": "text", |
| 217 | + "text": "row,health,status,index,uuid,pri(number of primary shards),rep(number of replica shards),docs.count(number of available documents),docs.deleted(number of deleted documents),store.size(store size of primary and replica shards),pri.store.size(store size of primary shards)\n1,green,open,.plugins-ml-config,nKyzDAupTGCwuybs9S_iBA,1,0,1,0,3.9kb,3.9kb\n2,green,open,.plugins-ml-mcp-tools,k1QwQKmXSeqRexmB2JDJiw,1,0,1,0,5kb,5kb\n" |
| 218 | + } |
| 219 | + ], |
| 220 | + "isError": false |
| 221 | + } |
| 222 | +} |
| 223 | +``` |
0 commit comments