Core component for exposing tools and resources to LLM applications
Core Idea: In Model Context Protocol (MCP), servers are specialized components that expose tools, resources, and prompts to host applications, enabling LLMs to access external functionality through a standardized interface.
Key Elements
Core Functions
- Tool Exposure: MCP servers define and expose executable functions (tools) that LLMs can call
- Resource Provision: They make documents and data accessible to host applications
- Communication Management: They handle JSON-RPC 2.0 message exchange with host applications
Server Categories
-
Official Reference Implementations:
- Showcase MCP features and SDK usage
- Maintained by Anthropic
- Designed for reliability and clarity
- Demonstrate protocol conformance
-
Official Integrations:
- Maintained by companies for their platforms
- Production-ready implementations
- Connect to specific services and APIs
- Often include authentication workflows
-
Community Servers:
- Developed by community members
- Cover diverse use cases and domains
- Varying levels of maintenance and support
- Unofficial and used at user's own risk
Implementation Steps
-
Choose SDK:
- TypeScript SDK for JavaScript/TypeScript development
- Python SDK for Python development (often using
fast-mcp
) - Community implementations in other languages
-
Define Server Logic:
- Create tools that perform actions or query capabilities
- Specify resources (documents or structured data)
- Implement authentication if required
-
Configure Transport:
- Server-Sent Events (SSE) for web-based clients
- Standard IO for local process communication
- Custom transport layers for specific environments
-
Handle Lifecycle:
- Respond to initialization and capability discovery requests
- Process tool calls and return results
- Manage resources and state as needed
Server Architecture
-
Component Structure:
- Servers are typically implemented as Python scripts using libraries like
fast-mcp
- They register tools, resources, and prompts with a standard API
- They use standardized I/O or server-sent events for communication
- Servers are typically implemented as Python scripts using libraries like
-
Lifecycle Management:
- Servers are discovered and launched by host applications
- They receive initialization requests when first connected
- They respond to capability discovery requests (listing available tools)
- They process tool calls and return results
Implementation Example
from model_context_protocol.server import Server
from model_context_protocol.fast_mcp import FastMCP
# Create server instance
server = FastMCP()
# Define and register tools
@server.tool("example_tool")
def example_tool(query: str) -> str:
# Tool implementation logic
return result
# Define and register resources
server.add_resource("example_resource", "path/to/resource.txt")
# Run the server with standard I/O
server.run(transport="stdio")
Express Servcer Implementation (Web Transport)
// Import necessary modules
import express from 'express';
import { MCPServer } from 'model-context-protocol';
// Create an Express server
const app = express();
const port = 881;
// Create MCP server instance
const server = new MCPServer();
// Define tools
server.addTool('getInventory', {
description: 'Get the product inventory',
parameters: {}, // Define parameters with Zod if needed
execute: async () => {
// Implementation logic to fetch inventory
return JSON.stringify(inventory);
}
});
// Set up SSE endpoint
app.get('/sse', (req, res) => {
// SSE setup code
// Establish the stateful connection between MCP client and server
});
// Set up messages endpoint
app.post('/sse/messages', (req, res) => {
// Handle incoming messages from MCP clients
// Process tool calls and return results
});
// Start the server
app.listen(port, () => {
console.log(`MCP Server running on port ${port}`);
});
Host Application Configuration
{
"mcpServers": {
"example": {
"command": "python",
"args": ["-m", "example_server"],
"env": {
"API_KEY": "<YOUR_API_KEY>"
}
}
}
}
Security Considerations
- Implement proper authentication
- Validate and sanitize inputs
- Set appropriate access controls
- Consider potential prompt injection vectors
- Implement rate limiting for resource-intensive operations
Testing and Deployment
- Test against the MCP specification
- Verify compatibility with clients
- Document usage patterns and examples
- Deploy as a standalone service or library
Key Reference Servers
- Fetch: Web content fetching and processing
- Filesystem: Secure file operations with access controls
- Memory: Knowledge graph-based persistent memory
- PostgreSQL/SQLite: Database interaction capabilities
- Time: Timezone and date/time processing
Connections
- Related Concepts: Model Context Protocol (MCP), MCP Tools, MCP Resources, MCP Architecture
- Implementation Examples: LangGraph Query Tool, Vector Store for Document Retrieval
- Compatible Libraries: LangChain Tools, fast-mcp, JSON-RPC 2.0
- Host Applications: Cursor IDE, Claude Desktop, Windsurf
- Development Tools: MCP Frameworks, MCP Transport Types (communication methods for MCP servers), MCP Client Implementation (how clients connect to these servers), Tool Call Flow in MCP (process of executing tools)
References
- Model Context Protocol Implementation Guide: modelcontextprotocol.io/introduction
- TypeScript SDK: github.com/modelcontextprotocol/typescript-sdk
- Python SDK: github.com/modelcontextprotocol/python-sdk
- MCP Servers Repository: github.com/modelcontextprotocol/servers
- MCP Specification: modelcontextprotocol.io/specification
#MCP #MCPServer #ToolIntegration #AITools #ServerImplementation #JSON-RPC #LLMIntegration
Sources: