MCP Course documentation

MCP SDK

Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

MCP SDK

The Model Context Protocol provides official SDKs for both JavaScript, Python and other languages. This makes it easy to implement MCP clients and servers in your applications. These SDKs handle the low-level protocol details, allowing you to focus on building your application’s capabilities.

SDK Overview

Both SDKs provide similar core functionality, following the MCP protocol specification we discussed earlier. They handle:

  • Protocol-level communication
  • Capability registration and discovery
  • Message serialization/deserialization
  • Connection management
  • Error handling

Core Primitives Implementation

Let’s explore how to implement each of the core primitives (Tools, Resources, and Prompts) using both SDKs.

python
javascript
from mcp.server.fastmcp import FastMCP

# Create an MCP server
mcp = FastMCP("Weather Service")


@mcp.tool()
def get_weather(location: str) -> str:
    """Get the current weather for a specified location."""
    return f"Weather in {location}: Sunny, 72°F"


@mcp.resource("weather://{location}")
def weather_resource(location: str) -> str:
    """Provide weather data as a resource."""
    return f"Weather data for {location}: Sunny, 72°F"


@mcp.prompt()
def weather_report(location: str) -> str:
    """Create a weather report prompt."""
    return f"""You are a weather reporter. Weather report for {location}?"""


# Run the server
if __name__ == "__main__":
    mcp.run()

Once you have your server implemented, you can start it by running the server script.

mcp dev server.py

This will initialize a development server running the file server.py. And log the following output:

Starting MCP inspector...
⚙️ Proxy server listening on port 6277
Spawned stdio transport
Connected MCP client to backing server transport
Created web app transport
Set up MCP proxy
🔍 MCP Inspector is up and running at http://127.0.0.1:6274 🚀

You can then open the MCP Inspector at http://127.0.0.1:6274 to see the server’s capabilities and interact with them.

You’ll see the server’s capabilities and the ability to call them via the UI.

MCP Inspector

MCP SDKs

MCP is designed to be language-agnostic, and there are official SDKs available for several popular programming languages:

Language Repository Maintainer(s) Status
TypeScript github.com/modelcontextprotocol/typescript-sdk Anthropic Active
Python github.com/modelcontextprotocol/python-sdk Anthropic Active
Java github.com/modelcontextprotocol/java-sdk Spring AI (VMware) Active
Kotlin github.com/modelcontextprotocol/kotlin-sdk JetBrains Active
C# github.com/modelcontextprotocol/csharp-sdk Microsoft Active (Preview)
Swift github.com/modelcontextprotocol/swift-sdk loopwork-ai Active
Rust github.com/modelcontextprotocol/rust-sdk Anthropic/Community Active

These SDKs provide language-specific abstractions that simplify working with the MCP protocol, allowing you to focus on implementing the core logic of your servers or clients rather than dealing with low-level protocol details.

Next Steps

We’ve only scratched the surface of what you can do with the MCP but you’ve already got a basic server running. In fact, you’ve also connected to it using the MCP Client in the browser.

In the next section, we’ll look at how to connect to your server from an LLM.

< > Update on GitHub