CH 48 / 86Chapter 6: MCP (The Standard for External Connections)

Building Your Own MCP Server

On this page8 items

In 10 seconds

With the SDK, adding a single decorator to a function turns it into a tool.
The hard part isn’t the code; it’s how you write the description.

For anything without an existing server — an internal inventory system, a custom core business system, a database used only by your department — you build one yourself.

It’s easier than you think.

A minimal server#

With the Python SDK, it looks like this.

text16 lines
from mcp.server.fastmcp import FastMCP

mcp = FastMCP("inventory")

@mcp.tool()
async def get_stock(item_code: str) -> str:
    """Look up the stock count for a product.

    Args:
        item_code: Product code (e.g. A-1024)
    """
    # Query the internal system here
    return f"There are 42 units of {item_code} in stock"

if __name__ == "__main__":
    mcp.run(transport="stdio")

That’s enough to make it work. A function decorated with @mcp.tool() becomes a tool Claude can call as is.

The argument type annotations and the docstring become the tool’s description directly. There’s no need to write a separate definition file. The SDK assembles the tools/list response for you.

To connect it, use stdio (#46).

text1 lines
claude mcp add --transport stdio inventory -- python /path/to/server.py

The hard part is the description#

Accuracy is determined more by how you write the docstring than by the code. What #20 covered applies directly here.

A bad example.

text3 lines
@mcp.tool()
async def get_stock(item_code: str) -> str:
    """Look up stock"""

A good example.

text10 lines
@mcp.tool()
async def get_stock(item_code: str) -> str:
    """Look up the current stock count for the given product code at the head-office warehouse.

    Does not include branch inventory. If you don't know the product code,
    use search_item first.

    Args:
        item_code: Product code in the form "A-1024". Includes a hyphen
    """

There are three differences. It says what it does not return (branches are not included). It says which tool to use first (search if the code is unknown). It describes the argument format concretely (includes a hyphen).

The third one matters most. As #20 explained, when arguments are missing, Claude sometimes fills them in by guessing. Making the format explicit reduces that guessing.

What to expose#

MCP can expose three things (#45). When you build your own, you’ll almost always use tools.

There is, however, one situation where resources shine: things like schemas or lists that don’t change from call to call but are large. Rather than fetching them with a tool every time, it’s more natural to keep them as a resource.

Start read-only#

Follow the criteria from #47 on the building side too.

Build only reading tools at first. Look up stock, search orders, check status. Add writes once reading is stable.

When you build a tool that writes, design in one of two things: make it reversible, or insert a confirmation step. Chapter 3, #19’s “for irreversible operations, a human presses the final button” becomes your own design responsibility here.

Testing#

While building, you can use the official development tool MCP Inspector. It lets you check the list of tools and their responses directly before connecting to Claude.

Once connected, check the status with claude mcp list and confirm it’s enabled with /mcp (#46). When it doesn’t work, this order is the fastest way to isolate the problem.

  1. In Inspector, does the server respond on its own?
  2. With claude mcp list, is it connected?
  3. With /context, are the tools loaded?
  4. If it still isn’t called, revisit the description

In my experience, you often end up at step 4.

Try it: have Claude grade a description (5 minutes)

Pick one internal system you’d like to connect. No implementation needed.

Prompt
“`
I’m designing the description (docstring) of a tool in an MCP server.

Tool: (what the tool does, in one line)

Write a description that includes the following.
・What the tool does
・What it does not return, and what is out of scope
・Whether another tool should be used first
・The concrete format of the arguments

Then list three situations where that description could cause it to be called by mistake.
“`

Expected result: you get the description and three weaknesses.

If it doesn’t work: if no weaknesses come out, add “assume someone is trying to misuse it.” Descriptions get better when you imagine the situations where it’s called by mistake rather than the ones where it’s used correctly.

Common pitfalls#

Piling on features from the start. Build ten tools and Claude will pick the wrong one. Build one, get it working, then add more.

Putting off the description. The description affects results more than working code does.

Building tools that write to internal systems first. Start with reading only.

Not returning errors as raw exceptions. If an exception is thrown, Claude can’t tell what happened. Return a readable string, like “Could not connect to the inventory system.”

Summary#

  • With the SDK, one decorator on a function makes it a tool
  • The hard part isn’t the code but the description. Include what it doesn’t return and the argument format
  • Start with reading tools. Add writes once things are stable
  • When a tool isn’t called, it’s usually a description problem

Read next#

In Practice: Connecting Market Data and Charts