AI Hints
Home›MCP Tutorial›Tool Inputs
MCP · Lesson 15 of 120

Defaults and Optional Inputs

Sensible defaults.

⏱ 2 min read▶ Video at 13:20🎓 Free
Watch this lesson in the MCP courseWatch on YouTube ↗Full MCP course

Not every input should be required. A search can default to three results, and a category can be left out. Defaults make tools easier for the model to call correctly.

Defaults and Optional Inputs
From the video: Defaults and Optional Inputs
  • Not all required
  • Defaults
  • Easier to call

Defaults in action

Limit defaults to three. Category is optional, and defaults to none. Only query is required. We call it twice, once with just a query, and once with everything. Run it. The first call uses the defaults.

defaults.pypython
from typing import Literal
from mcp.server.mcpserver import MCPServer
from tester import call

mcp = MCPServer("shop")

@mcp.tool()
def search(query: str, limit: int = 3, category: Literal["bikes", "parts"] | None = None) -> str:
    """Search Northwind's products."""
    return f"{limit} results for {query!r} in {category or 'everything'}"

for args in [{"query": "helmet"}, {"query": "tyre", "limit": 5, "category": "parts"}]:
    print(call(mcp, "search", args).content[0].text)
Output
3 results for 'helmet' in everything
5 results for 'tyre' in parts
Also uses tester.py
import asyncio
from mcp import Client

def call(server, tool, args=None, **options):
    async def go():
        async with Client(server, **options) as client:
            return await client.call_tool(tool, args or {})
    return asyncio.run(go())

def tools(server):
    async def go():
        async with Client(server) as client:
            return (await client.list_tools()).tools
    return asyncio.run(go())

Default tips

Required inputs are what the tool can't work without. Give everything else a safe default. Keep limits small by default, so results stay short. And use none for, no filter.

keyvalue
Requiredcan't work without
Defaultssafe and small
Noneno filter
Fewer inputsfewer mistakes

Summary

Give optional inputs sensible defaults, and only require what the tool truly needs. Next, Pydantic inputs.

Key takeaways
  • = default
  • | None
  • Required only if needed
Next up: Pydantic Inputs — rich arguments