116 lines
5.2 KiB
Markdown
116 lines
5.2 KiB
Markdown
# Programming custom MCP servers for AI Studio
|
|
|
|
## Built-in web toolset
|
|
|
|
In **Toolbox → MCP Servers**, choose **Activate Web Toolset**. 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 uses the SearXNG
|
|
search URL configured in **Toolbox → Web**. 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
|
|
a named server. For stdio, review and confirm process startup. For a URL,
|
|
choose **Discover Tools**. The composer shows the connected toolset and its tool
|
|
count. The connection stays active when you close Toolbox. Use **Disconnect**
|
|
to end it. 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 older `absdev-gitea` launch script is absent from its configured path.
|
|
A separate GiteaMCP project uses Streamable HTTP. If you run that service,
|
|
add a named `url` profile with the endpoint it advertises and configure the
|
|
service's credentials locally. A Streamable HTTP process cannot be substituted
|
|
for the missing stdio script.
|