Model Context Protocol

In this tutorial, we’ll build a simple MCP weather server and connect it to a host, Claude for Desktop.

What we’ll be building

We’ll build a server that exposes two tools: get_alerts and get_forecast. Then we’ll connect the server to an MCP host (in this case, Claude for Desktop):

Core MCP Concepts

MCP servers can provide three main types of capabilities:

  1. Resources: File-like data that can be read by clients (like API responses or file contents)
  2. Tools: Functions that can be called by the LLM (with user approval)
  3. Prompts: Pre-written templates that help users accomplish specific tasks

This tutorial will primarily focus on tools.

  • Python

  • TypeScript

  • Java

  • Kotlin

  • C#

  • Ruby

  • Rust

  • Go

Let’s get started with building our weather server! You can find the complete code for what we’ll be building here.

Prerequisite knowledge

This quickstart assumes you have familiarity with:

  • Python
  • LLMs like Claude

Logging in MCP Servers

When implementing MCP servers, be careful about how you handle logging:For STDIO-based servers: Never write to stdout. Writing to stdout will corrupt the JSON-RPC messages and break your server. The print() function writes to stdout by default, so keep it out of a STDIO server entirely.For HTTP-based servers: Standard output logging is fine since it doesn’t interfere with HTTP responses.

Best Practices

  • Use the standard library logging module, which writes to stderr.
  • Create one logger per module with logging.getLogger(__name__) and call it from your tools.

Quick Examples

System requirements

  • Python 3.10 or higher installed.
  • You must use the Python MCP SDK 2.0.0 or higher.

Set up your environment

First, let’s install uv and set up our Python project and environment:

Make sure to restart your terminal afterwards to ensure that the uv command gets picked up.Now, let’s create and set up our project:

Now let’s dive into building your server.

Building your server

Importing packages and setting up the instance

Add these to the top of your weather.py:

httpx2 is the HTTP client the SDK itself depends on, so installing mcp already brought it in.The MCPServer class uses Python type hints and docstrings to automatically generate tool definitions, making it easy to create and maintain MCP tools.

Helper functions

Next, let’s add our helper functions for querying and formatting the data from the National Weather Service API:

Implementing tool execution

The tool execution handler is responsible for actually executing the logic of each tool. Let’s add it:

Running the server

Finally, let’s initialize and run the server:

Your server is complete! Run uv run weather.py to start the MCP server, which will listen for messages from MCP hosts.Let’s now test your server from an existing MCP host, Claude for Desktop.

Testing your server with Claude for Desktop

First, make sure you have Claude for Desktop installed. You can install the latest version here. If you already have Claude for Desktop, make sure it’s updated to the latest version.We’ll need to configure Claude for Desktop for whichever MCP servers you want to use. To do this, open your Claude for Desktop App configuration at ~/Library/Application Support/Claude/claude_desktop_config.json in a text editor. Make sure to create the file if it doesn’t exist.For example, if you have VS Code installed:

You’ll then add your servers in the mcpServers key. The MCP UI elements will only show up in Claude for Desktop if at least one server is properly configured.In this case, we’ll add our single weather server like so:

This tells Claude for Desktop:

  1. There’s an MCP server named “weather”
  2. To launch it by running uv --directory /ABSOLUTE/PATH/TO/PARENT/FOLDER/weather run weather.py

Save the file, and restart Claude for Desktop.

Let’s get started with building our weather server! You can find the complete code for what we’ll be building here.

Prerequisite knowledge

This quickstart assumes you have familiarity with:

  • TypeScript
  • LLMs like Claude

Logging in MCP Servers

When implementing MCP servers, be careful about how you handle logging:For STDIO-based servers: Never use console.log(), as it writes to standard output (stdout) by default. Writing to stdout will corrupt the JSON-RPC messages and break your server.For HTTP-based servers: Standard output logging is fine since it doesn’t interfere with HTTP responses.

Best Practices

  • Use console.error() which writes to stderr, or use a logging library that writes to stderr or files.

Quick Examples

System requirements

For TypeScript, make sure you have the latest version of Node installed.

Set up your environment

First, let’s install Node.js and npm if you haven’t already. You can download them from nodejs.org. Verify your Node.js installation:

For this tutorial, you’ll need Node.js version 20 or higher.Now, let’s create and set up our project:

Update your package.json to add type: “module” and a build script:

package.json

Create a tsconfig.json in the root of your project:

tsconfig.json

Now let’s dive into building your server.

Building your server

Importing packages and setting up the instance

Add these to the top of your src/index.ts:

Helper functions

Next, let’s add our helper functions for querying and formatting the data from the National Weather Service API:

Implementing tool execution

The tool execution handler is responsible for actually executing the logic of each tool. Let’s add it:

Running the server

Finally, implement the main function to run the server:

Make sure to run npm run build to build your server! This is a very important step in getting your server to connect.Let’s now test your server from an existing MCP host, Claude for Desktop.

Testing your server with Claude for Desktop

First, make sure you have Claude for Desktop installed. You can install the latest version here. If you already have Claude for Desktop, make sure it’s updated to the latest version.We’ll need to configure Claude for Desktop for whichever MCP servers you want to use. To do this, open your Claude for Desktop App configuration at ~/Library/Application Support/Claude/claude_desktop_config.json in a text editor. Make sure to create the file if it doesn’t exist.For example, if you have VS Code installed:

You’ll then add your servers in the mcpServers key. The MCP UI elements will only show up in Claude for Desktop if at least one server is properly configured.In this case, we’ll add our single weather server like so:

This tells Claude for Desktop:

  1. There’s an MCP server named “weather”
  2. Launch it by running node /ABSOLUTE/PATH/TO/PARENT/FOLDER/weather/build/index.js

Save the file, and restart Claude for Desktop.

Let’s get started with building our weather server! You can find the complete code for what we’ll be building here.For more information, see the MCP Server Boot Starter reference documentation. For manual MCP Server implementation, refer to the MCP Server Java SDK documentation.

Logging in MCP Servers

When implementing MCP servers, be careful about how you handle logging:For STDIO-based servers: Never use System.out.println() or System.out.print(), as they write to standard output (stdout). Writing to stdout will corrupt the JSON-RPC messages and break your server.For HTTP-based servers: Standard output logging is fine since it doesn’t interfere with HTTP responses.

Best Practices

  • Use a logging library that writes to stderr or files.
  • Ensure any configured logging library will not write to stdout.

System requirements

Set up your environment

Use the Spring Initializer to bootstrap the project.You will need to add the following dependencies:

Then configure your application by setting the application properties:

The Server Configuration Properties documents all available properties.Now let’s dive into building your server.

Building your server

Weather Service

Let’s implement a WeatherService.java that uses a REST client to query the data from the National Weather Service API:

The @Service annotation will auto-register the service in your application context. The Spring AI @Tool annotation makes it easy to create and maintain MCP tools.The auto-configuration will automatically register these tools with the MCP server.

Create your Boot Application

Uses the MethodToolCallbackProvider utils to convert the @Tools into actionable callbacks used by the MCP server.

Running the server

Finally, let’s build the server:

This will generate an mcp-weather-stdio-server-0.0.1-SNAPSHOT.jar file within the target folder.Let’s now test your server from an existing MCP host, Claude for Desktop.

Testing your server with Claude for Desktop

First, make sure you have Claude for Desktop installed. You can install the latest version here. If you already have Claude for Desktop, make sure it’s updated to the latest version.We’ll need to configure Claude for Desktop for whichever MCP servers you want to use. To do this, open your Claude for Desktop App configuration at ~/Library/Application Support/Claude/claude_desktop_config.json in a text editor. Make sure to create the file if it doesn’t exist.For example, if you have VS Code installed:

You’ll then add your servers in the mcpServers key. The MCP UI elements will only show up in Claude for Desktop if at least one server is properly configured.In this case, we’ll add our single weather server like so:

This tells Claude for Desktop:

  1. There’s an MCP server named “my-weather-server”
  2. To launch it by running java -jar /ABSOLUTE/PATH/TO/PARENT/FOLDER/mcp-weather-stdio-server-0.0.1-SNAPSHOT.jar

Save the file, and restart Claude for Desktop.

Testing your server with Java client

Create an MCP Client manually

Use the McpClient to connect to the server:

Use MCP Client Boot Starter

Create a new boot starter application using the spring-ai-starter-mcp-client dependency:

and set the spring.ai.mcp.client.stdio.servers-configuration property to point to your claude_desktop_config.json. You can reuse the existing Anthropic Desktop configuration:

When you start your client application, the auto-configuration will automatically create MCP clients from the claude_desktop_config.json.For more information, see the MCP Client Boot Starters reference documentation.

More Java MCP Server examples

The starter-webflux-server demonstrates how to create an HTTP-based MCP server with the WebFlux starter. Set the spring.ai.mcp.server.protocol=STREAMABLE property to serve it over Streamable HTTP. It showcases how to define and register MCP Tools, Resources, and Prompts, using the Spring Boot’s auto-configuration capabilities.

Let’s get started with building our weather server! You can find the complete code for what we’ll be building here.

Prerequisite knowledge

This quickstart assumes you have familiarity with:

  • Kotlin
  • LLMs like Claude

Logging in MCP Servers

When implementing MCP servers, be careful about how you handle logging:For STDIO-based servers: Never use println(), as it writes to standard output (stdout) by default. Writing to stdout will corrupt the JSON-RPC messages and break your server.For HTTP-based servers: Standard output logging is fine since it doesn’t interfere with HTTP responses.

Best Practices

  • Use a logging library that writes to stderr or files.

System requirements

  • JDK 11 or higher installed.

Set up your environment

First, let’s install java and gradle if you haven’t already. You can download java from official Oracle JDK website. Verify your java installation:

Now, let’s create and set up your project:

After running gradle init, select Application as the project type, Kotlin as the programming language.Alternatively, you can create a Kotlin application using the IntelliJ IDEA project wizard.After creating the project, replace the contents of your build.gradle.kts with:

build.gradle.kts

Verify that everything is set up correctly:

Now let’s dive into building your server.

Building your server

Setting up the instance

Add a server initialization function:

Weather API helper functions

Next, let’s add functions and data classes for querying and converting responses from the National Weather Service API:

Implementing tool execution

The tool execution handler is responsible for actually executing the logic of each tool. Let’s add it:

Running the server

Finally, implement the main function to run the server:

You can run the server directly during development:

For production use, build the shadow JAR:

Let’s now test your server from an existing MCP host, Claude for Desktop.

Testing your server with Claude for Desktop

First, make sure you have Claude for Desktop installed. You can install the latest version here. If you already have Claude for Desktop, make sure it’s updated to the latest version.We’ll need to configure Claude for Desktop for whichever MCP servers you want to use. To do this, open your Claude for Desktop App configuration at ~/Library/Application Support/Claude/claude_desktop_config.json in a text editor. Make sure to create the file if it doesn’t exist.For example, if you have VS Code installed:

You’ll then add your servers in the mcpServers key. The MCP UI elements will only show up in Claude for Desktop if at least one server is properly configured.In this case, we’ll add our single weather server like so:

This tells Claude for Desktop:

  1. There’s an MCP server named “weather”
  2. Launch it by running java -jar /ABSOLUTE/PATH/TO/PARENT/FOLDER/weather/build/libs/weather-0.1.0-all.jar

Save the file, and restart Claude for Desktop.

Let’s get started with building our weather server! You can find the complete code for what we’ll be building here.

Prerequisite knowledge

This quickstart assumes you have familiarity with:

  • C#
  • LLMs like Claude
  • .NET 8 or higher

Logging in MCP Servers

When implementing MCP servers, be careful about how you handle logging:For STDIO-based servers: Never use Console.WriteLine() or Console.Write(), as they write to standard output (stdout). Writing to stdout will corrupt the JSON-RPC messages and break your server.For HTTP-based servers: Standard output logging is fine since it doesn’t interfere with HTTP responses.

Best Practices

  • Use a logging library that writes to stderr or files.

System requirements

Set up your environment

First, let’s install dotnet if you haven’t already. You can download dotnet from official Microsoft .NET website. Verify your dotnet installation:

Now, let’s create and set up your project:

After running dotnet new console, you will be presented with a new C# project. You can open the project in your favorite IDE, such as Visual Studio or Rider. Alternatively, you can create a C# application using the Visual Studio project wizard. After creating the project, add NuGet package for the Model Context Protocol SDK and hosting:

Now let’s dive into building your server.

Building your server

Open the Program.cs file in your project and replace its contents with the following code:

This code sets up a basic console application that uses the Model Context Protocol SDK to create an MCP server with standard I/O transport.

Weather API helper functions

Create an extension class for HttpClient which helps simplify JSON request handling:

Next, define a class with the tool execution handlers for querying and converting responses from the National Weather Service API:

Running the server

Finally, run the server using the following command:

This will start the server and listen for incoming requests on standard input/output.

Testing your server with Claude for Desktop

First, make sure you have Claude for Desktop installed. You can install the latest version here. If you already have Claude for Desktop, make sure it’s updated to the latest version. We’ll need to configure Claude for Desktop for whichever MCP servers you want to use. To do this, open your Claude for Desktop App configuration at ~/Library/Application Support/Claude/claude_desktop_config.json in a text editor. Make sure to create the file if it doesn’t exist. For example, if you have VS Code installed:

You’ll then add your servers in the mcpServers key. The MCP UI elements will only show up in Claude for Desktop if at least one server is properly configured. In this case, we’ll add our single weather server like so:

This tells Claude for Desktop:

  1. There’s an MCP server named “weather”
  2. Launch it by running dotnet run /ABSOLUTE/PATH/TO/PROJECT Save the file, and restart Claude for Desktop.

Let’s get started with building our weather server! You can find the complete code for what we’ll be building here.

Prerequisite knowledge

This quickstart assumes you have familiarity with:

  • Ruby
  • LLMs like Claude

Logging in MCP Servers

When implementing MCP servers, be careful about how you handle logging:For STDIO-based servers: Never use puts or print, as they write to standard output (stdout) by default. Writing to stdout will corrupt the JSON-RPC messages and break your server.For HTTP-based servers: Standard output logging is fine since it doesn’t interfere with HTTP responses.

Best Practices

  • Use a logging library that writes to stderr or files.

Quick Examples

System requirements

  • Ruby 2.7 or higher installed.

Set up your environment

First, let’s make sure you have Ruby installed. You can check by running:

Now, let’s create and set up our project:

Now let’s dive into building your server.

Building your server

Importing packages and setting up constants

Open weather.rb and add these requires and constants at the top:

The mcp gem provides the Model Context Protocol SDK for Ruby, with classes for server implementation and stdio transport.

Helper methods

Next, let’s add helper methods for querying and formatting data from the National Weather Service API:

Implementing tool execution

Now let’s define our tool classes. Each tool subclasses MCP::Tool and implements the tool logic:

Running the server

Finally, initialize and run the server:

Your server is complete! Run bundle exec ruby weather.rb to start the MCP server, which will listen for messages from MCP hosts.Let’s now test your server from an existing MCP host, Claude for Desktop.

Testing your server with Claude for Desktop

First, make sure you have Claude for Desktop installed. You can install the latest version here. If you already have Claude for Desktop, make sure it’s updated to the latest version.We’ll need to configure Claude for Desktop for whichever MCP servers you want to use. To do this, open your Claude for Desktop App configuration at ~/Library/Application Support/Claude/claude_desktop_config.json in a text editor. Make sure to create the file if it doesn’t exist.For example, if you have VS Code installed:

You’ll then add your servers in the mcpServers key. The MCP UI elements will only show up in Claude for Desktop if at least one server is properly configured.In this case, we’ll add our single weather server like so:

This tells Claude for Desktop:

  1. There’s an MCP server named “weather”
  2. Launch it by running bundle exec ruby weather.rb in the specified directory

Save the file, and restart Claude for Desktop.

Let’s get started with building our weather server! You can find the complete code for what we’ll be building here.

Prerequisite knowledge

This quickstart assumes you have familiarity with:

  • Rust programming language
  • Async/await in Rust
  • LLMs like Claude

Logging in MCP Servers

When implementing MCP servers, be careful about how you handle logging:For STDIO-based servers: Never use println!() or print!(), as they write to standard output (stdout). Writing to stdout will corrupt the JSON-RPC messages and break your server.For HTTP-based servers: Standard output logging is fine since it doesn’t interfere with HTTP responses.

Best Practices

  • Use a logging library that writes to stderr or files, such as tracing or log in Rust.
  • Configure your logging framework to avoid stdout output.

Quick Examples

System requirements

  • Rust 1.70 or higher installed.
  • Cargo (comes with Rust installation).

Set up your environment

First, let’s install Rust if you haven’t already. You can install Rust from rust-lang.org:

Verify your Rust installation:

Now, let’s create and set up our project:

Update your Cargo.toml to add the required dependencies:

Cargo.toml

Now let’s dive into building your server.

Building your server

Importing packages and constants

Open src/main.rs and add these imports and constants at the top:

The rmcp crate provides the Model Context Protocol SDK for Rust, with features for server implementation, procedural macros, and stdio transport.

Data structures

Next, let’s define the data structures for deserializing responses from the National Weather Service API:

Now define the request types that MCP clients will send:

Helper functions

Add helper functions for making API requests and formatting responses:

Implementing the Weather server and tools

Now let’s implement the main Weather server struct with the tool handlers:

The #[tool_router] macro automatically generates the routing logic, and the #[tool] attribute marks methods as MCP tools.

Implementing the ServerHandler

Implement the ServerHandler trait to define server capabilities:

Running the server

Finally, implement the main function to run the server with stdio transport:

Build your server with:

The compiled binary will be in target/release/weather.Let’s now test your server from an existing MCP host, Claude for Desktop.

Testing your server with Claude for Desktop

First, make sure you have Claude for Desktop installed. You can install the latest version here. If you already have Claude for Desktop, make sure it’s updated to the latest version.We’ll need to configure Claude for Desktop for whichever MCP servers you want to use. To do this, open your Claude for Desktop App configuration at ~/Library/Application Support/Claude/claude_desktop_config.json in a text editor. Make sure to create the file if it doesn’t exist.For example, if you have VS Code installed:

You’ll then add your servers in the mcpServers key. The MCP UI elements will only show up in Claude for Desktop if at least one server is properly configured.In this case, we’ll add our single weather server like so:

This tells Claude for Desktop:

  1. There’s an MCP server named “weather”
  2. Launch it by running the compiled binary at the specified path

Save the file, and restart Claude for Desktop.

Let’s get started with building our weather server! You can find the complete code for what we’ll be building here.

Prerequisite knowledge

This quickstart assumes you have familiarity with:

  • Go
  • LLMs like Claude

Logging in MCP Servers

When implementing MCP servers, be careful about how you handle logging:For STDIO-based servers: Never use fmt.Println() or fmt.Printf(), as they write to standard output (stdout). Writing to stdout will corrupt the JSON-RPC messages and break your server.For HTTP-based servers: Standard output logging is fine since it doesn’t interfere with HTTP responses.

Best Practices

  • Use log.Println() (which defaults to stderr) or a logging library that writes to stderr or files.
  • Use fmt.Fprintf(os.Stderr, ...) to write to stderr explicitly.

Quick Examples

System requirements

  • Go 1.24 or higher installed.

Set up your environment

First, let’s install Go if you haven’t already. You can download and install Go from go.dev.Verify your Go installation:

Now, let’s create and set up our project:

Now let’s dive into building your server.

Building your server

Importing packages and constants

Add these to the top of your main.go:

Data structures

Next, let’s define the data structures used by our tools:

Helper functions

Next, let’s add our helper functions for querying and formatting the data from the National Weather Service API:

Implementing tool execution

The tool execution handler is responsible for actually executing the logic of each tool. Let’s add it:

Running the server

Finally, implement the main function to run the server:

Build your server with:

The compiled binary will be in ./weather.Let’s now test your server from an existing MCP host, Claude for Desktop.

Testing your server with Claude for Desktop

First, make sure you have Claude for Desktop installed. You can install the latest version here. If you already have Claude for Desktop, make sure it’s updated to the latest version.We’ll need to configure Claude for Desktop for whichever MCP servers you want to use. To do this, open your Claude for Desktop App configuration at ~/Library/Application Support/Claude/claude_desktop_config.json in a text editor. Make sure to create the file if it doesn’t exist.For example, if you have VS Code installed:

You’ll then add your servers in the mcpServers key. The MCP UI elements will only show up in Claude for Desktop if at least one server is properly configured.In this case, we’ll add our single weather server like so:

This tells Claude for Desktop:

  1. There’s an MCP server named “weather”
  2. Launch it by running the compiled binary at the specified path

Save the file, and restart Claude for Desktop.

Test with commands

Let’s make sure Claude for Desktop is picking up the two tools we’ve exposed in our weather server. You can do this by looking for the “Add files, connectors, and more /” icon:

After clicking on the plus icon, hover over the “Connectors” menu. You should see the weather servers listed:

If your server isn’t being picked up by Claude for Desktop, proceed to the Troubleshooting section for debugging tips. If the server has shown up in the “Connectors” menu, you can now test your server by running the following commands in Claude for Desktop:

  • What’s the weather in Sacramento?
  • What are the active weather alerts in Texas?

What’s happening under the hood

When you ask a question:

  1. The client sends your question to Claude
  2. Claude analyzes the available tools and decides which one(s) to use
  3. The client executes the chosen tool(s) through the MCP server
  4. The results are sent back to Claude
  5. Claude formulates a natural language response
  6. The response is displayed to you!

Troubleshooting

Next steps

Read the original on modelcontextprotocol.io ↗