Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Creating MCP Server | Setting Up and Configuration
Creating Custom AI Agents with Anthropic Claude

bookCreating MCP Server

An MCP server is simply a Python script. To create one, start by making a .py file using any text editor. A good option is Visual Studio Code, which is free, beginner-friendly, and supports Python well. If you haven’t installed it yet, visit the official website, download the version for your system, and follow the setup instructions.

Once in Visual Studio Code, create a new file and name it something like server.py. This file will hold your server logic.

import.py

import.py

copy

To actually create the server, you'll need to instantiate the FastMCP class. Initially, this can be done by accessing it through the full module path like so:

main.py

main.py

copy

However, constantly typing the full path is inconvenient. A cleaner and more readable approach is to change the import statement itself. Instead of importing the entire module, you can write:

main.py

main.py

copy

You now have a basic server instance stored in the variable my_mcp. This server can be customized by passing in various optional arguments such as a name, instructions, or a list of tools. For now, it's enough to assign a name so you can keep track of different servers if you build more than one.

main.py

main.py

copy

If you run the script, it finishes right away even with a print statement. That’s because the server isn't told to stay active. To fix this, call the run() method on your server variable at the end of the script. This keeps the server online and ready for tasks.

main.py

main.py

copy

At this point, the server is running but doesn't do anything yet. To make it useful, you need to define at least one tool or a function the AI can call to perform a task.

For example, AI models like Claude can't access real-time data on their own. If you ask What time is it?, Claude will say it doesn't know. But with an MCP server, you can fix this limitation.

You don't need to write the code yourself. Just ask Claude to generate a function that returns the current time. Paste it into your script after creating the server. This makes the server ready to handle tasks.

Claude will generate a function, you can adjust prompt as you like to get closer results to what you want. Alternately you can just simply copy this.

main.py

main.py

copy

To make this function usable by the server, you need to add a decorator. This is a special line of code that goes above the function and starts with @. You don't need to understand how it works, just know that it tells the server: this function is a tool.

file1

file1

copy
Note
Study More

If you want to understand how decorators work in Python and when to use them effectively, you can check out this article How to use Decorators in Python.

question mark

What is the main purpose of calling my_mcp.run() in the script?

Select the correct answer

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 2. Kapitel 3

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

Suggested prompts:

Can you show me how to add the decorator to my function?

What does the decorator actually do in this context?

How do I connect my MCP server to Claude after adding the tool?

Awesome!

Completion rate improved to 11.11

bookCreating MCP Server

Stryg for at vise menuen

An MCP server is simply a Python script. To create one, start by making a .py file using any text editor. A good option is Visual Studio Code, which is free, beginner-friendly, and supports Python well. If you haven’t installed it yet, visit the official website, download the version for your system, and follow the setup instructions.

Once in Visual Studio Code, create a new file and name it something like server.py. This file will hold your server logic.

import.py

import.py

copy

To actually create the server, you'll need to instantiate the FastMCP class. Initially, this can be done by accessing it through the full module path like so:

main.py

main.py

copy

However, constantly typing the full path is inconvenient. A cleaner and more readable approach is to change the import statement itself. Instead of importing the entire module, you can write:

main.py

main.py

copy

You now have a basic server instance stored in the variable my_mcp. This server can be customized by passing in various optional arguments such as a name, instructions, or a list of tools. For now, it's enough to assign a name so you can keep track of different servers if you build more than one.

main.py

main.py

copy

If you run the script, it finishes right away even with a print statement. That’s because the server isn't told to stay active. To fix this, call the run() method on your server variable at the end of the script. This keeps the server online and ready for tasks.

main.py

main.py

copy

At this point, the server is running but doesn't do anything yet. To make it useful, you need to define at least one tool or a function the AI can call to perform a task.

For example, AI models like Claude can't access real-time data on their own. If you ask What time is it?, Claude will say it doesn't know. But with an MCP server, you can fix this limitation.

You don't need to write the code yourself. Just ask Claude to generate a function that returns the current time. Paste it into your script after creating the server. This makes the server ready to handle tasks.

Claude will generate a function, you can adjust prompt as you like to get closer results to what you want. Alternately you can just simply copy this.

main.py

main.py

copy

To make this function usable by the server, you need to add a decorator. This is a special line of code that goes above the function and starts with @. You don't need to understand how it works, just know that it tells the server: this function is a tool.

file1

file1

copy
Note
Study More

If you want to understand how decorators work in Python and when to use them effectively, you can check out this article How to use Decorators in Python.

question mark

What is the main purpose of calling my_mcp.run() in the script?

Select the correct answer

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 2. Kapitel 3
some-alt