# {frontmatter.title}In this chapter, we’ll learn how to write a simple MCP Server. We’ll create a server that provides basic user information such as name, age, location, and interests.
Basic Concepts
An MCP (Model Control Protocol) Server is a server that extends Cursor IDE’s functionality. It allows us to define custom tools that can be called by Cursor to retrieve information or perform specific actions.
Writing the Code
Let’s open our main.py file and write the following code:
from mcp.server.fastmcp import FastMCP
# Initialize FastMCP server
mcp = FastMCP("about-me")
@mcp.tool()
async def get_user_name() -> str:
"""Get the user's name.
"""
return "Lucas Lee"
@mcp.tool()
async def get_user_age() -> int:
"""Get the user's age.
"""
return 30
@mcp.tool()
async def get_user_location() -> str:
"""Get the user's location.
"""
return "San Francisco, CA"
@mcp.tool()
async def get_user_interests() -> list[str]:
"""Get the user's interests.
"""
return ["travel", "food", "technology"]
if __name__ == "__main__":
# Initialize and run the server
mcp.run(transport='stdio')
Let's break down this code:
## Code Explanation
1. **Importing Required Module**
```python
from mcp.server.fastmcp import FastMCPWe import the FastMCP class from the MCP library, which is the main class for creating an MCP server.
- Initializing the Server
mcp = FastMCP("about-me")
Create a new MCP server instance named "about-me".
3. **Defining Tool Functions**
```python
@mcp.tool()
async def get_user_name() -> str:- Use the
@mcp.tool()decorator to mark this function as an MCP tool - Functions are
asyncbecause MCP server is asynchronous - Use type annotations
-> strto specify return type
Implementing Information Retrieval
get_user_name(): Returns the user’s nameget_user_age(): Returns the user’s ageget_user_location(): Returns the user’s locationget_user_interests(): Returns a list of user’s interests
Starting the Server
if __name__ == "__main__": mcp.run(transport='stdio')
- Use `mcp.run()` to start the server
- `transport='stdio'` specifies using standard input/output for communication
## Customizing Your Information
You can customize the returned values according to your needs:
- Modify `get_user_name()` to return your actual name
- Add your real interests to `get_user_interests()`
- Update `get_user_location()` to your location
## Running the Server
After writing the code, you can run the server in your terminal:
```bash
python main.py
## Next Steps
In the next chapter, we'll learn how to:
- Test the MCP server
- Add more custom functionality
- Handle errors and exceptions
- Add configuration options
## Common Issues
1. **Server Won't Start?**
- Ensure virtual environment is activated
- Check all dependencies are installed
- Verify there are no syntax errors
2. **Function Return Type Errors?**
- Ensure return values match type annotations
- Strings use `str`
- Numbers use `int` or `float`
- Lists use `list[type]`