MCP Builder
MicrosoftOfficialPublished by Microsoft
- Skill
- Guidelines
- Software development
- MIT
- Low risk
- Has scripts
AutoGineer summary
Written by AutoGineer from the skill’s own files, not by the author
mcp-builder provides a structured, four-phase workflow for developing MCP servers that enable LLMs to interact with external services. It covers the entire lifecycle from initial research and planning, through implementation in TypeScript, Python, or C#, to testing and creating robust evaluations. The guide emphasizes best practices for tool design, schema definition, and error handling to ensure agents can effectively utilize the exposed functionality.
What it does
- Provides a step-by-step process for building MCP servers.
- Offers guidance on selecting the right language SDK (TypeScript, Python, C#).
- Defines standards for tool naming, schema design, and context management.
- Includes instructions for implementing core infrastructure like authentication and pagination.
- Outlines a methodology for creating XML-based evaluations to test agent performance.
- Provides references for Microsoft-specific MCP patterns and Azure integration.
When to use it
- When you need to integrate custom internal APIs or third-party SaaS into an LLM agent.
- When you are building a new MCP server and want to follow industry best practices.
- When you need to evaluate the effectiveness of your MCP server's tools.
- When deciding between building a custom server or using existing Microsoft MCP infrastructure.
Advantages
- Standardizes the development process for consistent, high-quality MCP servers.
- Promotes the use of structured schemas for better agent interaction.
- Includes a clear path for testing and validating server performance.
- Supports multiple programming languages and transport mechanisms.
Usage examples
- “I need to build an MCP server for my internal CRM API. What is the recommended workflow?”
- The agent will guide you through the four-phase process: planning your tool definitions, selecting the appropriate SDK, implementing the server with proper error handling, and creating an evaluation suite.
- “How should I structure my tool definitions for an MCP server?”
- The agent will provide patterns for clear, action-oriented naming, using Zod or Pydantic for input schemas, and defining output schemas to help agents process data effectively.
Install
Download the skill as a ZIP:
https://mp-staging.autogineer.com/agent-skills/microsoft/mcp-builder/downloadUnzip it into the cross-client skills folder of your project:
mkdir -p .agents/skills && unzip mcp-builder.zip -d .agents/skills
Documentation by Microsoft
MCP Server Development Guide
Overview
Create MCP (Model Context Protocol) servers that enable LLMs to interact with external services through well-designed tools. The quality of an MCP server is measured by how well it enables LLMs to accomplish real-world tasks.
Microsoft MCP Ecosystem
Microsoft provides extensive MCP infrastructure for Azure and Foundry services. Understanding this ecosystem helps you decide whether to build custom servers or leverage existing ones.
Server Types
| Type | Transport | Use Case | Example |
|---|---|---|---|
| Local | stdio | Desktop apps, single-user, local dev | Azure MCP Server via NPM/Docker |
| Remote | Streamable HTTP | Cloud services, multi-tenant, Agent Service | https://mcp.ai.azure.com (Foundry) |
Microsoft MCP Servers
Before building a custom server, check if Microsoft already provides one:
| Server | Type | Description |
|---|---|---|
| Azure MCP | Local | 48+ Azure services (Storage, KeyVault, Cosmos, SQL, etc.) |
| Foundry MCP | Remote | https://mcp.ai.azure.com - Models, deployments, evals, agents |
| Fabric MCP | Local | Microsoft Fabric APIs, OneLake, item definitions |
| Playwright MCP | Local | Browser automation and testing |
| GitHub MCP | Remote | https://api.githubcopilot.com/mcp |
Full ecosystem: See 🔷 Microsoft MCP Patterns for complete server catalog and patterns.
When to Use Microsoft vs Custom
| Scenario | Recommendation |
|---|---|
| Azure service integration | Use Azure MCP Server (48 services covered) |
| AI Foundry agents/evals | Use Foundry MCP remote server |
| Custom internal APIs | Build custom server (this guide) |
| Third-party SaaS integration | Build custom server (this guide) |
| Extending Azure MCP | Follow Microsoft MCP Patterns |
Process
🚀 High-Level Workflow
Creating a high-quality MCP server involves four main phases:
Phase 1: Deep Research and Planning
1.1 Understand Modern MCP Design
API Coverage vs. Workflow Tools: Balance comprehensive API endpoint coverage with specialized workflow tools. Workflow tools can be more convenient for specific tasks, while comprehensive coverage gives agents flexibility to compose operations. Performance varies by client—some clients benefit from code execution that combines basic tools, while others work better with higher-level workflows. When uncertain, prioritize comprehensive API coverage.
Tool Naming and Discoverability:
Clear, descriptive tool names help agents find the right tools quickly. Use consistent prefixes (e.g., github_create_issue, github_list_repos) and action-oriented naming.
Context Management: Agents benefit from concise tool descriptions and the ability to filter/paginate results. Design tools that return focused, relevant data. Some clients support code execution which can help agents filter and process data efficiently.
Actionable Error Messages: Error messages should guide agents toward solutions with specific suggestions and next steps.
1.2 Study MCP Protocol Documentation
Navigate the MCP specification:
Start with the sitemap to find relevant pages: https://modelcontextprotocol.io/sitemap.xml
Then fetch specific pages with .md suffix for markdown format (e.g., https://modelcontextprotocol.io/specification/draft.md).
Key pages to review:
Specification overview and architecture
Transport mechanisms (streamable HTTP, stdio)
Tool, resource, and prompt definitions
1.3 Study Framework Documentation
Language Selection:
| Language | Best For | SDK |
|---|---|---|
| TypeScript (recommended) | General MCP servers, broad compatibility | @modelcontextprotocol/sdk |
| Python | Data/ML pipelines, FastAPI integration | mcp (FastMCP) |
| C#/.NET | Azure/Microsoft ecosystem, enterprise | Microsoft.Mcp.Core |
Transport Selection:
| Transport | Use Case | Characteristics |
|---|---|---|
| Streamable HTTP | Remote servers, multi-tenant, Agent Service | Stateless, scalable, requires auth |
| stdio | Local servers, desktop apps | Simple, single-user, no network |
Load framework documentation:
MCP Best Practices: 📋 View Best Practices - Core guidelines
For TypeScript (recommended):
TypeScript SDK: Use WebFetch to load
https://raw.githubusercontent.com/modelcontextprotocol/typescript-sdk/main/README.md⚡ TypeScript Guide - TypeScript patterns and examples
For Python:
Python SDK: Use WebFetch to load
https://raw.githubusercontent.com/modelcontextprotocol/python-sdk/main/README.md🐍 Python Guide - Python patterns and examples
For C#/.NET (Microsoft ecosystem):
🔷 Microsoft MCP Patterns - C# patterns, Azure MCP architecture, command hierarchy
1.4 Plan Your Implementation
Understand the API: Review the service's API documentation to identify key endpoints, authentication requirements, and data models. Use web search and WebFetch as needed.
Tool Selection: Prioritize comprehensive API coverage. List endpoints to implement, starting with the most common operations.
Phase 2: Implementation
2.1 Set Up Project Structure
See language-specific guides for project setup:
⚡ TypeScript Guide - Project structure, package.json, tsconfig.json
🐍 Python Guide - Module organization, dependencies
🔷 Microsoft MCP Patterns - C# project structure, command hierarchy
2.2 Implement Core Infrastructure
Create shared utilities:
API client with authentication
Error handling helpers
Response formatting (JSON/Markdown)
Pagination support
2.3 Implement Tools
For each tool:
Input Schema:
Use Zod (TypeScript) or Pydantic (Python)
Include constraints and clear descriptions
Add examples in field descriptions
Output Schema:
Define
outputSchemawhere possible for structured dataUse
structuredContentin tool responses (TypeScript SDK feature)Helps clients understand and process tool outputs
Tool Description:
Concise summary of functionality
Parameter descriptions
Return type schema
Implementation:
Async/await for I/O operations
Proper error handling with actionable messages
Support pagination where applicable
Return both text content and structured data when using modern SDKs
Annotations:
readOnlyHint: true/falsedestructiveHint: true/falseidempotentHint: true/falseopenWorldHint: true/false
Phase 3: Review and Test
3.1 Code Quality
Review for:
No duplicated code (DRY principle)
Consistent error handling
Full type coverage
Clear tool descriptions
3.2 Build and Test
TypeScript:
Run
npm run buildto verify compilationTest with MCP Inspector:
npx @modelcontextprotocol/inspector
Python:
Verify syntax:
python -m py_compile your_server.pyTest with MCP Inspector
See language-specific guides for detailed testing approaches and quality checklists.
Phase 4: Create Evaluations
After implementing your MCP server, create comprehensive evaluations to test its effectiveness.
Load ✅ Evaluation Guide for complete evaluation guidelines.
4.1 Understand Evaluation Purpose
Use evaluations to test whether LLMs can effectively use your MCP server to answer realistic, complex questions.
4.2 Create 10 Evaluation Questions
To create effective evaluations, follow the process outlined in the evaluation guide:
Tool Inspection: List available tools and understand their capabilities
Content Exploration: Use READ-ONLY operations to explore available data
Question Generation: Create 10 complex, realistic questions
Answer Verification: Solve each question yourself to verify answers
4.3 Evaluation Requirements
Ensure each question is:
Independent: Not dependent on other questions
Read-only: Only non-destructive operations required
Complex: Requiring multiple tool calls and deep exploration
Realistic: Based on real use cases humans would care about
Verifiable: Single, clear answer that can be verified by string comparison
Stable: Answer won't change over time
4.4 Output Format
Create an XML file with this structure:
<evaluation>
<qa_pair>
<question>Find discussions about AI model launches with animal codenames. One model needed a specific safety designation that uses the format ASL-X. What number X was being determined for the model named after a spotted wild cat?</question>
<answer>3</answer>
</qa_pair>
<!-- More qa_pairs... -->
</evaluation>Reference Files
📚 Documentation Library
Load these resources as needed during development:
Core MCP Documentation (Load First)
MCP Protocol: Start with sitemap at
https://modelcontextprotocol.io/sitemap.xml, then fetch specific pages with.mdsuffix📋 MCP Best Practices - Universal MCP guidelines including:
Server and tool naming conventions
Response format guidelines (JSON vs Markdown)
Pagination best practices
Transport selection (streamable HTTP vs stdio)
Security and error handling standards
Microsoft MCP Documentation (For Azure/Foundry)
🔷 Microsoft MCP Patterns - Microsoft-specific patterns including:
Azure MCP Server architecture (48+ Azure services)
C#/.NET command implementation patterns
Remote MCP with Foundry Agent Service
Authentication (Entra ID, OBO flow, Managed Identity)
Testing infrastructure with Bicep templates
SDK Documentation (Load During Phase 1/2)
Python SDK: Fetch from
https://raw.githubusercontent.com/modelcontextprotocol/python-sdk/main/README.mdTypeScript SDK: Fetch from
https://raw.githubusercontent.com/modelcontextprotocol/typescript-sdk/main/README.mdMicrosoft MCP SDK: See Microsoft MCP Patterns for C#/.NET
Language-Specific Implementation Guides (Load During Phase 2)
🐍 Python Implementation Guide - Complete Python/FastMCP guide with:
Server initialization patterns
Pydantic model examples
Tool registration with
@mcp.toolComplete working examples
Quality checklist
⚡ TypeScript Implementation Guide - Complete TypeScript guide with:
Project structure
Zod schema patterns
Tool registration with
server.registerToolComplete working examples
Quality checklist
🔷 Microsoft MCP Patterns - Complete C#/.NET guide with:
Command hierarchy (BaseCommand → GlobalCommand → SubscriptionCommand)
Naming conventions (
{Resource}{Operation}Command)Option handling with
.AsRequired()/.AsOptional()Azure Functions remote MCP deployment
Live test patterns with Bicep
Evaluation Guide (Load During Phase 4)
✅ Evaluation Guide - Complete evaluation creation guide with:
Question creation guidelines
Answer verification strategies
XML format specifications
Example questions and answers
Running an evaluation with the provided scripts
Files
- ATTRIBUTION.mdAdded by AutoGineer853 B
- LICENSELicence file added by AutoGineer1.1 KiB
- SKILL.md11.9 KiB
- reference/evaluation.md21.2 KiB
- reference/mcp_best_practices.md12.1 KiB
- reference/microsoft_mcp_patterns.md21.0 KiB
- reference/node_mcp_server.md28.5 KiB
- reference/python_mcp_server.md25.1 KiB
- scripts/connections.pyScript4.8 KiB
- scripts/evaluation.pyScript12.3 KiB
- scripts/example_evaluation.xml1.2 KiB
- scripts/requirements.txt29 B
reference/python_mcp_server.md
Python MCP Server Implementation Guide
Overview
This document provides Python-specific best practices and examples for implementing MCP servers using the MCP Python SDK. It covers server setup, tool registration patterns, input validation with Pydantic, error handling, and complete working examples.
Quick Reference
Key Imports
from mcp.server.fastmcp import FastMCP
from pydantic import BaseModel, Field, field_validator, ConfigDict
from typing import Optional, List, Dict, Any
from enum import Enum
import httpxServer Initialization
mcp = FastMCP("service_mcp")Tool Registration Pattern
@mcp.tool(name="tool_name", annotations={...})
async def tool_function(params: InputModel) -> str:
# Implementation
passMCP Python SDK and FastMCP
The official MCP Python SDK provides FastMCP, a high-level framework for building MCP servers. It provides:
Automatic description and inputSchema generation from function signatures and docstrings
Pydantic model integration for input validation
Decorator-based tool registration with
@mcp.tool
For complete SDK documentation, use WebFetch to load:
https://raw.githubusercontent.com/modelcontextprotocol/python-sdk/main/README.md
Server Naming Convention
Python MCP servers must follow this naming pattern:
Format:
{service}_mcp(lowercase with underscores)Examples:
github_mcp,jira_mcp,stripe_mcp
The name should be:
General (not tied to specific features)
Descriptive of the service/API being integrated
Easy to infer from the task description
Without version numbers or dates
Tool Implementation
Tool Naming
Use snake_case for tool names (e.g., "search_users", "create_project", "get_channel_info") with clear, action-oriented names.
Avoid Naming Conflicts: Include the service context to prevent overlaps:
Use "slack_send_message" instead of just "send_message"
Use "github_create_issue" instead of just "create_issue"
Use "asana_list_tasks" instead of just "list_tasks"
Tool Structure with FastMCP
Tools are defined using the @mcp.tool decorator with Pydantic models for input validation:
from pydantic import BaseModel, Field, ConfigDict
from mcp.server.fastmcp import FastMCP
# Initialize the MCP server
mcp = FastMCP("example_mcp")
# Define Pydantic model for input validation
class ServiceToolInput(BaseModel):
'''Input model for service tool operation.'''
model_config = ConfigDict(
str_strip_whitespace=True, # Auto-strip whitespace from strings
validate_assignment=True, # Validate on assignment
extra='forbid' # Forbid extra fields
)
param1: str = Field(..., description="First parameter description (e.g., 'user123', 'project-abc')", min_length=1, max_length=100)
param2: Optional[int] = Field(default=None, description="Optional integer parameter with constraints", ge=0, le=1000)
tags: Optional[List[str]] = Field(default_factory=list, description="List of tags to apply", max_items=10)
@mcp.tool(
name="service_tool_name",
annotations={
"title": "Human-Readable Tool Title",
"readOnlyHint": True, # Tool does not modify environment
"destructiveHint": False, # Tool does not perform destructive operations
"idempotentHint": True, # Repeated calls have no additional effect
"openWorldHint": False # Tool does not interact with external entities
}
)
async def service_tool_name(params: ServiceToolInput) -> str:
'''Tool description automatically becomes the 'description' field.
This tool performs a specific operation on the service. It validates all inputs
using the ServiceToolInput Pydantic model before processing.
Args:
params (ServiceToolInput): Validated input parameters containing:
- param1 (str): First parameter description
- param2 (Optional[int]): Optional parameter with default
- tags (Optional[List[str]]): List of tags
Returns:
str: JSON-formatted response containing operation results
'''
# Implementation here
passPydantic v2 Key Features
Use
model_configinstead of nestedConfigclassUse
field_validatorinstead of deprecatedvalidatorUse
model_dump()instead of deprecateddict()Validators require
@classmethoddecoratorType hints are required for validator methods
from pydantic import BaseModel, Field, field_validator, ConfigDict
class CreateUserInput(BaseModel):
model_config = ConfigDict(
str_strip_whitespace=True,
validate_assignment=True
)
name: str = Field(..., description="User's full name", min_length=1, max_length=100)
email: str = Field(..., description="User's email address", pattern=r'^[\w\.-]+@[\w\.-]+\.\w+$')
age: int = Field(..., description="User's age", ge=0, le=150)
@field_validator('email')
@classmethod
def validate_email(cls, v: str) -> str:
if not v.strip():
raise ValueError("Email cannot be empty")
return v.lower()Response Format Options
Support multiple output formats for flexibility:
from enum import Enum
class ResponseFormat(str, Enum):
'''Output format for tool responses.'''
MARKDOWN = "markdown"
JSON = "json"
class UserSearchInput(BaseModel):
query: str = Field(..., description="Search query")
response_format: ResponseFormat = Field(
default=ResponseFormat.MARKDOWN,
description="Output format: 'markdown' for human-readable or 'json' for machine-readable"
)Markdown format:
Use headers, lists, and formatting for clarity
Convert timestamps to human-readable format (e.g., "2024-01-15 10:30:00 UTC" instead of epoch)
Show display names with IDs in parentheses (e.g., "@john.doe (U123456)")
Omit verbose metadata (e.g., show only one profile image URL, not all sizes)
Group related information logically
JSON format:
Return complete, structured data suitable for programmatic processing
Include all available fields and metadata
Use consistent field names and types
Pagination Implementation
For tools that list resources:
class ListInput(BaseModel):
limit: Optional[int] = Field(default=20, description="Maximum results to return", ge=1, le=100)
offset: Optional[int] = Field(default=0, description="Number of results to skip for pagination", ge=0)
async def list_items(params: ListInput) -> str:
# Make API request with pagination
data = await api_request(limit=params.limit, offset=params.offset)
# Return pagination info
response = {
"total": data["total"],
"count": len(data["items"]),
"offset": params.offset,
"items": data["items"],
"has_more": data["total"] > params.offset + len(data["items"]),
"next_offset": params.offset + len(data["items"]) if data["total"] > params.offset + len(data["items"]) else None
}
return json.dumps(response, indent=2)Error Handling
Provide clear, actionable error messages:
def _handle_api_error(e: Exception) -> str:
'''Consistent error formatting across all tools.'''
if isinstance(e, httpx.HTTPStatusError):
if e.response.status_code == 404:
return "Error: Resource not found. Please check the ID is correct."
elif e.response.status_code == 403:
return "Error: Permission denied. You don't have access to this resource."
elif e.response.status_code == 429:
return "Error: Rate limit exceeded. Please wait before making more requests."
return f"Error: API request failed with status {e.response.status_code}"
elif isinstance(e, httpx.TimeoutException):
return "Error: Request timed out. Please try again."
return f"Error: Unexpected error occurred: {type(e).__name__}"Shared Utilities
Extract common functionality into reusable functions:
# Shared API request function
async def _make_api_request(endpoint: str, method: str = "GET", **kwargs) -> dict:
'''Reusable function for all API calls.'''
async with httpx.AsyncClient() as client:
response = await client.request(
method,
f"{API_BASE_URL}/{endpoint}",
timeout=30.0,
**kwargs
)
response.raise_for_status()
return response.json()Async/Await Best Practices
Always use async/await for network requests and I/O operations:
# Good: Async network request
async def fetch_data(resource_id: str) -> dict:
async with httpx.AsyncClient() as client:
response = await client.get(f"{API_URL}/resource/{resource_id}")
response.raise_for_status()
return response.json()
# Bad: Synchronous request
def fetch_data(resource_id: str) -> dict:
response = requests.get(f"{API_URL}/resource/{resource_id}") # Blocks
return response.json()Type Hints
Use type hints throughout:
from typing import Optional, List, Dict, Any
async def get_user(user_id: str) -> Dict[str, Any]:
data = await fetch_user(user_id)
return {"id": data["id"], "name": data["name"]}Tool Docstrings
Every tool must have comprehensive docstrings with explicit type information:
async def search_users(params: UserSearchInput) -> str:
'''
Search for users in the Example system by name, email, or team.
This tool searches across all user profiles in the Example platform,
supporting partial matches and various search filters. It does NOT
create or modify users, only searches existing ones.
Args:
params (UserSearchInput): Validated input parameters containing:
- query (str): Search string to match against names/emails (e.g., "john", "@example.com", "team:marketing")
- limit (Optional[int]): Maximum results to return, between 1-100 (default: 20)
- offset (Optional[int]): Number of results to skip for pagination (default: 0)
Returns:
str: JSON-formatted string containing search results with the following schema:
Success response:
{
"total": int, # Total number of matches found
"count": int, # Number of results in this response
"offset": int, # Current pagination offset
"users": [
{
"id": str, # User ID (e.g., "U123456789")
"name": str, # Full name (e.g., "John Doe")
"email": str, # Email address (e.g., "[email protected]")
"team": str # Team name (e.g., "Marketing") - optional
}
]
}
Error response:
"Error: <error message>" or "No users found matching '<query>'"
Examples:
- Use when: "Find all marketing team members" -> params with query="team:marketing"
- Use when: "Search for John's account" -> params with query="john"
- Don't use when: You need to create a user (use example_create_user instead)
- Don't use when: You have a user ID and need full details (use example_get_user instead)
Error Handling:
- Input validation errors are handled by Pydantic model
- Returns "Error: Rate limit exceeded" if too many requests (429 status)
- Returns "Error: Invalid API authentication" if API key is invalid (401 status)
- Returns formatted list of results or "No users found matching 'query'"
'''Complete Example
See below for a complete Python MCP server example:
#!/usr/bin/env python3
'''
MCP Server for Example Service.
This server provides tools to interact with Example API, including user search,
project management, and data export capabilities.
'''
from typing import Optional, List, Dict, Any
from enum import Enum
import httpx
from pydantic import BaseModel, Field, field_validator, ConfigDict
from mcp.server.fastmcp import FastMCP
# Initialize the MCP server
mcp = FastMCP("example_mcp")
# Constants
API_BASE_URL = "https://api.example.com/v1"
# Enums
class ResponseFormat(str, Enum):
'''Output format for tool responses.'''
MARKDOWN = "markdown"
JSON = "json"
# Pydantic Models for Input Validation
class UserSearchInput(BaseModel):
'''Input model for user search operations.'''
model_config = ConfigDict(
str_strip_whitespace=True,
validate_assignment=True
)
query: str = Field(..., description="Search string to match against names/emails", min_length=2, max_length=200)
limit: Optional[int] = Field(default=20, description="Maximum results to return", ge=1, le=100)
offset: Optional[int] = Field(default=0, description="Number of results to skip for pagination", ge=0)
response_format: ResponseFormat = Field(default=ResponseFormat.MARKDOWN, description="Output format")
@field_validator('query')
@classmethod
def validate_query(cls, v: str) -> str:
if not v.strip():
raise ValueError("Query cannot be empty or whitespace only")
return v.strip()
# Shared utility functions
async def _make_api_request(endpoint: str, method: str = "GET", **kwargs) -> dict:
'''Reusable function for all API calls.'''
async with httpx.AsyncClient() as client:
response = await client.request(
method,
f"{API_BASE_URL}/{endpoint}",
timeout=30.0,
**kwargs
)
response.raise_for_status()
return response.json()
def _handle_api_error(e: Exception) -> str:
'''Consistent error formatting across all tools.'''
if isinstance(e, httpx.HTTPStatusError):
if e.response.status_code == 404:
return "Error: Resource not found. Please check the ID is correct."
elif e.response.status_code == 403:
return "Error: Permission denied. You don't have access to this resource."
elif e.response.status_code == 429:
return "Error: Rate limit exceeded. Please wait before making more requests."
return f"Error: API request failed with status {e.response.status_code}"
elif isinstance(e, httpx.TimeoutException):
return "Error: Request timed out. Please try again."
return f"Error: Unexpected error occurred: {type(e).__name__}"
# Tool definitions
@mcp.tool(
name="example_search_users",
annotations={
"title": "Search Example Users",
"readOnlyHint": True,
"destructiveHint": False,
"idempotentHint": True,
"openWorldHint": True
}
)
async def example_search_users(params: UserSearchInput) -> str:
'''Search for users in the Example system by name, email, or team.
[Full docstring as shown above]
'''
try:
# Make API request using validated parameters
data = await _make_api_request(
"users/search",
params={
"q": params.query,
"limit": params.limit,
"offset": params.offset
}
)
users = data.get("users", [])
total = data.get("total", 0)
if not users:
return f"No users found matching '{params.query}'"
# Format response based on requested format
if params.response_format == ResponseFormat.MARKDOWN:
lines = [f"# User Search Results: '{params.query}'", ""]
lines.append(f"Found {total} users (showing {len(users)})")
lines.append("")
for user in users:
lines.append(f"## {user['name']} ({user['id']})")
lines.append(f"- **Email**: {user['email']}")
if user.get('team'):
lines.append(f"- **Team**: {user['team']}")
lines.append("")
return "\n".join(lines)
else:
# Machine-readable JSON format
import json
response = {
"total": total,
"count": len(users),
"offset": params.offset,
"users": users
}
return json.dumps(response, indent=2)
except Exception as e:
return _handle_api_error(e)
if __name__ == "__main__":
mcp.run()Advanced FastMCP Features
Context Parameter Injection
FastMCP can automatically inject a Context parameter into tools for advanced capabilities like logging, progress reporting, resource reading, and user interaction:
from mcp.server.fastmcp import FastMCP, Context
mcp = FastMCP("example_mcp")
@mcp.tool()
async def advanced_search(query: str, ctx: Context) -> str:
'''Advanced tool with context access for logging and progress.'''
# Report progress for long operations
await ctx.report_progress(0.25, "Starting search...")
# Log information for debugging
await ctx.log_info("Processing query", {"query": query, "timestamp": datetime.now()})
# Perform search
results = await search_api(query)
await ctx.report_progress(0.75, "Formatting results...")
# Access server configuration
server_name = ctx.fastmcp.name
return format_results(results)
@mcp.tool()
async def interactive_tool(resource_id: str, ctx: Context) -> str:
'''Tool that can request additional input from users.'''
# Request sensitive information when needed
api_key = await ctx.elicit(
prompt="Please provide your API key:",
input_type="password"
)
# Use the provided key
return await api_call(resource_id, api_key)Context capabilities:
ctx.report_progress(progress, message)- Report progress for long operationsctx.log_info(message, data)/ctx.log_error()/ctx.log_debug()- Loggingctx.elicit(prompt, input_type)- Request input from usersctx.fastmcp.name- Access server configurationctx.read_resource(uri)- Read MCP resources
Resource Registration
Expose data as resources for efficient, template-based access:
@mcp.resource("file://documents/{name}")
async def get_document(name: str) -> str:
'''Expose documents as MCP resources.
Resources are useful for static or semi-static data that doesn't
require complex parameters. They use URI templates for flexible access.
'''
document_path = f"./docs/{name}"
with open(document_path, "r") as f:
return f.read()
@mcp.resource("config://settings/{key}")
async def get_setting(key: str, ctx: Context) -> str:
'''Expose configuration as resources with context.'''
settings = await load_settings()
return json.dumps(settings.get(key, {}))When to use Resources vs Tools:
Resources: For data access with simple parameters (URI templates)
Tools: For complex operations with validation and business logic
Structured Output Types
FastMCP supports multiple return types beyond strings:
from typing import TypedDict
from dataclasses import dataclass
from pydantic import BaseModel
# TypedDict for structured returns
class UserData(TypedDict):
id: str
name: str
email: str
@mcp.tool()
async def get_user_typed(user_id: str) -> UserData:
'''Returns structured data - FastMCP handles serialization.'''
return {"id": user_id, "name": "John Doe", "email": "[email protected]"}
# Pydantic models for complex validation
class DetailedUser(BaseModel):
id: str
name: str
email: str
created_at: datetime
metadata: Dict[str, Any]
@mcp.tool()
async def get_user_detailed(user_id: str) -> DetailedUser:
'''Returns Pydantic model - automatically generates schema.'''
user = await fetch_user(user_id)
return DetailedUser(**user)Lifespan Management
Initialize resources that persist across requests:
from contextlib import asynccontextmanager
@asynccontextmanager
async def app_lifespan():
'''Manage resources that live for the server's lifetime.'''
# Initialize connections, load config, etc.
db = await connect_to_database()
config = load_configuration()
# Make available to all tools
yield {"db": db, "config": config}
# Cleanup on shutdown
await db.close()
mcp = FastMCP("example_mcp", lifespan=app_lifespan)
@mcp.tool()
async def query_data(query: str, ctx: Context) -> str:
'''Access lifespan resources through context.'''
db = ctx.request_context.lifespan_state["db"]
results = await db.query(query)
return format_results(results)Transport Options
FastMCP supports two main transport mechanisms:
# stdio transport (for local tools) - default
if __name__ == "__main__":
mcp.run()
# Streamable HTTP transport (for remote servers)
if __name__ == "__main__":
mcp.run(transport="streamable_http", port=8000)Transport selection:
stdio: Command-line tools, local integrations, subprocess execution
Streamable HTTP: Web services, remote access, multiple clients
Code Best Practices
Code Composability and Reusability
Your implementation MUST prioritize composability and code reuse:
Extract Common Functionality:
Create reusable helper functions for operations used across multiple tools
Build shared API clients for HTTP requests instead of duplicating code
Centralize error handling logic in utility functions
Extract business logic into dedicated functions that can be composed
Extract shared markdown or JSON field selection & formatting functionality
Avoid Duplication:
NEVER copy-paste similar code between tools
If you find yourself writing similar logic twice, extract it into a function
Common operations like pagination, filtering, field selection, and formatting should be shared
Authentication/authorization logic should be centralized
Python-Specific Best Practices
Use Type Hints: Always include type annotations for function parameters and return values
Pydantic Models: Define clear Pydantic models for all input validation
Avoid Manual Validation: Let Pydantic handle input validation with constraints
Proper Imports: Group imports (standard library, third-party, local)
Error Handling: Use specific exception types (httpx.HTTPStatusError, not generic Exception)
Async Context Managers: Use
async withfor resources that need cleanupConstants: Define module-level constants in UPPER_CASE
Quality Checklist
Before finalizing your Python MCP server implementation, ensure:
Strategic Design
[ ] Tools enable complete workflows, not just API endpoint wrappers
[ ] Tool names reflect natural task subdivisions
[ ] Response formats optimize for agent context efficiency
[ ] Human-readable identifiers used where appropriate
[ ] Error messages guide agents toward correct usage
Implementation Quality
[ ] FOCUSED IMPLEMENTATION: Most important and valuable tools implemented
[ ] All tools have descriptive names and documentation
[ ] Return types are consistent across similar operations
[ ] Error handling is implemented for all external calls
[ ] Server name follows format:
{service}_mcp[ ] All network operations use async/await
[ ] Common functionality is extracted into reusable functions
[ ] Error messages are clear, actionable, and educational
[ ] Outputs are properly validated and formatted
Tool Configuration
[ ] All tools implement 'name' and 'annotations' in the decorator
[ ] Annotations correctly set (readOnlyHint, destructiveHint, idempotentHint, openWorldHint)
[ ] All tools use Pydantic BaseModel for input validation with Field() definitions
[ ] All Pydantic Fields have explicit types and descriptions with constraints
[ ] All tools have comprehensive docstrings with explicit input/output types
[ ] Docstrings include complete schema structure for dict/JSON returns
[ ] Pydantic models handle input validation (no manual validation needed)
Advanced Features (where applicable)
[ ] Context injection used for logging, progress, or elicitation
[ ] Resources registered for appropriate data endpoints
[ ] Lifespan management implemented for persistent connections
[ ] Structured output types used (TypedDict, Pydantic models)
[ ] Appropriate transport configured (stdio or streamable HTTP)
Code Quality
[ ] File includes proper imports including Pydantic imports
[ ] Pagination is properly implemented where applicable
[ ] Filtering options are provided for potentially large result sets
[ ] All async functions are properly defined with
async def[ ] HTTP client usage follows async patterns with proper context managers
[ ] Type hints are used throughout the code
[ ] Constants are defined at module level in UPPER_CASE
Testing
[ ] Server runs successfully:
python your_server.py --help[ ] All imports resolve correctly
[ ] Sample tool calls work as expected
[ ] Error scenarios handled gracefully
Related Resources
Other Language Guides
TypeScript: Node/TypeScript MCP Server Guide - Zod schemas, Express.js patterns
C#/.NET: Microsoft MCP Patterns - Azure MCP Server architecture, command hierarchy, Foundry integration
Microsoft-Specific Patterns
If building MCP servers for Azure services or integrating with Foundry Agent Service, see Microsoft MCP Patterns for:
Azure Functions remote MCP deployment
Entra ID authentication patterns
Tool approval and Agent Service integration
C# command implementation patterns
Requirements and compatibility
Written for: Claude Code
Works with: Claude Code, Claude apps, OpenAI Codex, GitHub Copilot, Cursor, Gemini CLI, Kiro, OpenCode, Goose, Amp, Roo Code, JetBrains Junie, Factory Droid, Devin (ex-Windsurf), Mistral Vibe, OpenHands, Letta
“Works with” means the tool reads the open Agent Skills format; a skill written for one tool may use features others lack.
Needs: Names Claude Code tools, Needs an MCP server, Needs network access, Needs Python, Needs Node.js
Security
What our scanner noticed· Low risk
- Instructions that use the network
- Scripts that your agent may run
This skill includes scripts that your agent may run. Read them before you use it.
A static scan is not a guarantee. Skills are third-party content: review a skill before you let an agent use it.
Attribution and licence
- Published by
- Microsoft
- Publisher
- Microsoft (Official)
- Source
- microsoft/skills · .github/skills/mcp-builder · commit 0696600
- Licence
- MIT License (redistribution allowed)
- LICENSE (added by AutoGineer from LICENSE)
- ATTRIBUTION.md (written by AutoGineer)
- Synced
AutoGineer is not affiliated with or endorsed by Microsoft.
Download ZIP
The ZIP holds the skill’s files, its licence files and an ATTRIBUTION.md. Download mcp-builder.zip