119 lines
5.6 KiB
Markdown
119 lines
5.6 KiB
Markdown
# Programming custom MCP servers for AI Studio
|
||
|
||
## Built-in web toolset
|
||
|
||
In **Toolbox → MCP Servers**, choose **Connect** on **Built-In Web**. This loads
|
||
`retrieve_page` and `web_search` inside AI Studio without launching an MCP
|
||
subprocess. Choose a tool, review its JSON input and confirm the call.
|
||
Page retrieval takes a `url`. Search takes a `query` and defaults to Bing. Configure a SearXNG
|
||
search URL in **Toolbox → Web** to override the default. You can attach returned sources
|
||
to the next chat message.
|
||
|
||
## Connect an existing server
|
||
|
||
Open **Toolbox** beside the composer. Choose **Use LM Studio Config** to read
|
||
`~/.lmstudio/mcp.json`, or **Import mcp.json** to choose another file. Select
|
||
**Connect** on the row for the server you want. For stdio, choose **Start &
|
||
Connect** to launch the executable and discover tools in one step. Each row
|
||
shows its own connection status. Multiple servers may stay connected, and
|
||
tool calls on different servers can run concurrently. Use **Tools** on a
|
||
connected row to open its tool inspector. The composer shows the total
|
||
number of connected toolsets. The composer shows the connected toolset and its tool
|
||
count. The connection stays active when you close Toolbox. Use **Disconnect** on a server’s row to end that connection. AI Studio remembers the configuration file and the selected
|
||
profile. The source file remains your editable configuration.
|
||
|
||
A local profile accepts `command`, an `args` array, `env`, and an optional
|
||
absolute `cwd`. Each argument stays a separate string. No shell interprets the
|
||
config. Named commands resolve against PATH and common macOS binary folders.
|
||
For an ordinary `python` or `python3` command with an absolute script path,
|
||
AI Studio also checks for a nearby `.venv` unless the profile sets PATH.
|
||
Use an explicit virtual-environment executable for reproducibility.
|
||
|
||
A remote profile takes a Streamable HTTP `url` and optional `headers`.
|
||
Use HTTPS for remote hosts. HTTP is supported on loopback. Keep bearer
|
||
credentials in your selected file or in the manual session-only field.
|
||
Copy examples with placeholders and supply your own values locally.
|
||
|
||
```json
|
||
{
|
||
"mcpServers": {
|
||
"my-local-knowledge": {
|
||
"command": "/absolute/project/.venv/bin/python",
|
||
"args": ["-u", "/absolute/project/server.py"],
|
||
"cwd": "/absolute/project",
|
||
"env": { "PYTHONUNBUFFERED": "1" }
|
||
},
|
||
"my-remote-tools": {
|
||
"url": "https://tools.example.com/mcp",
|
||
"headers": { "Authorization": "Bearer YOUR_TOKEN" }
|
||
}
|
||
}
|
||
}
|
||
```
|
||
|
||
The attached archive's `src/knowledge_mcp.py` matches the installed
|
||
`local-knowledge` program. Select that profile to discover
|
||
`search_knowledge`, which accepts `query` and an optional `limit`. Run a query
|
||
in Toolbox, review the returned passages, then attach the result to chat.
|
||
The MCP index remains with its server. AI Studio's native **Knowledge Stacks**
|
||
can separately index files you import in Workspace.
|
||
|
||
## Implement a server
|
||
|
||
Use an official MCP SDK. For stdio, communicate in newline-delimited UTF-8
|
||
JSON-RPC on stdout and write logs on stderr. Support initialize, the initialized
|
||
notification, tools/list and tools/call. Return a JSON Schema object for each
|
||
tool's input, validate arguments server-side, and return structured output or
|
||
content entries. Paginate discovery with nextCursor if your listing is large.
|
||
|
||
A small Python example using the Python SDK's FastMCP interface:
|
||
|
||
```python
|
||
from mcp.server.fastmcp import FastMCP
|
||
|
||
server = FastMCP("Example reference tools")
|
||
|
||
@server.tool()
|
||
def lookup(term: str) -> str:
|
||
"""Return a short glossary entry."""
|
||
glossary = {"SQLite": "An embedded relational database."}
|
||
return glossary.get(term, "No matching entry.")
|
||
|
||
if __name__ == "__main__":
|
||
server.run(transport="stdio")
|
||
```
|
||
|
||
Use your virtual environment's Python and select the executable plus script
|
||
in an MCP profile. See the [MCP stdio and HTTP specification](https://modelcontextprotocol.io/specification/2025-11-25/basic/transports)
|
||
and the [LM Studio configuration format](https://lmstudio.ai/docs/app/mcp).
|
||
|
||
## Invoke and troubleshoot
|
||
|
||
- Select a discovered tool and inspect **Input Schema**. Enter JSON arguments
|
||
and confirm the call. Tool runs are manual. You can attach the resulting JSON
|
||
to the next reply in the active chat.
|
||
- **Executable unavailable:** set the absolute binary path or check the
|
||
profile's PATH. A GUI app may start with a different PATH than Terminal.
|
||
- **Configured script does not exist:** correct its path in the source
|
||
configuration and choose **Reload**. AI Studio cannot find files moved out
|
||
of a previous working directory.
|
||
- **Server closes or times out:** inspect the error and captured stderr.
|
||
Confirm the virtual environment has the dependencies. Keep startup banners
|
||
and logs off stdout. The stdio client waits 30 seconds per request.
|
||
- **HTTP 401/403:** check the configured authorization header and token.
|
||
Confirm that the URL is the MCP endpoint rather than the service homepage.
|
||
- **Discovery works but a tool fails:** compare your JSON with its input
|
||
schema and check the server's own logs. The app sends an invocation once.
|
||
|
||
The HTTP client supports legacy and stateless discovery. The stdio client
|
||
supports legacy protocol initialization. OAuth negotiation, server-requested
|
||
follow-up inputs, and automatic model-driven dispatch are outside this implementation.
|
||
|
||
### Existing Gitea profile
|
||
|
||
The obsolete stdio script can be replaced by a working local GiteaMCP
|
||
checkout. If it normally starts in HTTP mode, use its Streamable HTTP URL
|
||
profile, or use a Python entry point that starts its MCP object in stdio
|
||
mode. Give the server an interpreter with the dependencies it expects.
|
||
Reload the configuration in Toolbox when you change it.
|