Build an MCP server that exposes tools, and do not write logs to stdout
Answer: Build a small MCP server that exposes tools a host can call. Log to stderr, because writing to stdout will corrupt the JSON-RPC messages and break your server. A host such as Gemini CLI can then discover and execute those tools.
Why it matters
The official tutorial builds a simple MCP weather server and connects it to a host. We'll build a server that exposes two tools: get_alerts and get_forecast. MCP servers can provide three main types of capabilities, including tools, which are functions that can be called by the LLM with user approval. When you test from a desktop host, pass the absolute path to your server.
On stdio transports, writing to stdout will corrupt the JSON-RPC messages and break your server. Use the standard library logging module, which writes to stderr. Create one logger per module and call it from your tools. For HTTP-based servers, standard output logging is fine because it does not interfere with HTTP responses.
An MCP server is an application that exposes tools and resources to the Gemini CLI. It can list tool descriptions and parameters through standardized schema definitions, and it can execute tools with defined arguments.
Expose toolsThe tutorial server exposesget_alertsandget_forecastLog to stderrWriting to stdout will corrupt the JSON-RPC messagesHost discoveryList available tools, their descriptions, and parameters
See also
Claims
- id: C1 claim: Writing to stdout will corrupt JSON-RPC messages and break a stdio MCP server. source: S1 evidence: "Writing to stdout will corrupt the JSON-RPC messages and break your server."
- id: C2 claim: The official tutorial builds a server that exposes get_alerts and get_forecast tools. source: S1 evidence: "We'll build a server that exposes two tools:
get_alertsandget_forecast."
- id: C3 claim: An MCP server exposes tools and resources to Gemini CLI through the protocol. source: S2 evidence: "An MCP server is an application that exposes tools and resources"
Sources
- [S1] Build an MCP server. Model Context Protocol. https://modelcontextprotocol.io/docs/2026-07-28/develop/build-server . License: CC-BY-4.0. Accessed 2026-11-22.
- [S2] MCP servers with Gemini CLI. Gemini CLI. https://github.com/google-gemini/gemini-cli/blob/main/docs/tools/mcp-server.md . License: Apache-2.0. Accessed 2026-11-22.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Model Context Protocol, Build an MCP server, CC BY 4.0; Gemini CLI, MCP servers, Apache-2.0. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Model Context Protocol, Build an MCP server, CC BY 4.0; Gemini CLI, MCP servers, Apache-2.0.
Cite this
iinventedlight. 2026. "Build an MCP server that exposes tools, and do not write logs to stdout." iinventedlight Library, iil:note:f3c7g4vk, version of 2026-11-22. https://iinventedlight.com/library/notes/building-an-mcp-server/. License: CC-BY-4.0.
@misc{iil_note_f3c7g4vk,
author = {{iinventedlight}},
title = {{Build an MCP server that exposes tools, and do not write logs to stdout}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-22},
url = {https://iinventedlight.com/library/notes/building-an-mcp-server/},
note = {Permanent ID iil:note:f3c7g4vk. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/building-an-mcp-server.md.