Difference between revisions of "MCP Fundamentals"

From CSE330 Wiki
Jump to navigationJump to search
 
Line 150: Line 150:
  
 
Claude will automatically call your tools when needed.
 
Claude will automatically call your tools when needed.
 +
 +
 +
== Example MCP Tool: hello ==
 +
 +
Below is a simple example of an MCP tool:
 +
 +
<source lang="python">
 +
@mcp.tool()
 +
async def hello(name: str = "world") -> str:
 +
    """Return a simple greeting to verify the server is working."""
 +
    return f"Hello CSE 3300 Student, {name}!"
 +
</source>
 +
 +
This function defines a tool that Claude Desktop can call through the MCP server.
 +
 +
----
 +
 +
== What This Function Does ==
 +
 +
This tool returns a simple greeting message. It is primarily used to verify that:
 +
 +
* your MCP server is running
 +
* Docker is working correctly
 +
* Claude Desktop is connected to your server
 +
 +
If this tool works, your MCP setup is likely correct.
 +
 +
----
 +
 +
== Line-by-Line Explanation ==
 +
 +
=== @mcp.tool() ===
 +
 +
This is a '''decorator''' that registers the function as an MCP tool.
 +
 +
Without this line, Claude will not be able to see or use the function.
 +
 +
----
 +
 +
=== async def hello(...) ===
 +
 +
This defines the function.
 +
 +
* <code>hello</code> is the '''tool name'''
 +
* Claude will refer to this function by this name when calling it
 +
 +
The keyword <code>async</code> indicates that the function runs within the MCP system. You do not need to fully understand asynchronous programming for this assignment.
 +
 +
----
 +
 +
=== name: str = "world" ===
 +
 +
This defines an input parameter:
 +
 +
* <code>name</code> is the parameter name
 +
* <code>str</code> indicates it should be a string
 +
* <code>"world"</code> is the default value
 +
 +
If no value is provided, the function will use "world".
 +
 +
----
 +
 +
=== -> str ===
 +
 +
This is a return type hint.
 +
 +
It indicates that the function returns a string.
 +
 +
Note: Python does not strictly enforce this, but it is important for clarity.
 +
 +
----
 +
 +
=== Docstring ===
 +
 +
<source lang="python">
 +
"""Return a simple greeting to verify the server is working."""
 +
</source>
 +
 +
This is a '''docstring'''.
 +
 +
It serves two purposes:
 +
 +
* explains the function to humans
 +
* helps Claude understand what the tool does and when to use it
 +
 +
----
 +
 +
=== return statement ===
 +
 +
<source lang="python">
 +
return f"Hello CSE 3300 Student, {name}!"
 +
</source>
 +
 +
This returns the output of the tool.
 +
 +
For example:
 +
 +
<pre>
 +
hello("Todd") → Hello CSE 3300 Student, Todd!
 +
</pre>
 +
 +
----
 +
 +
== How Claude Uses This Tool ==
 +
 +
When you type a prompt such as:
 +
 +
<pre>
 +
Use CardinalsMCP to say hello to Todd
 +
</pre>
 +
 +
Claude will:
 +
 +
# recognize that the <code>hello</code> tool is relevant
 +
# call the function with <code>name="Todd"</code>
 +
# receive the returned string
 +
# display the result in the chat
 +
 +
----
 +
 +
== Key Takeaway ==
 +
 +
An MCP tool is just a Python function that:
 +
 +
* is decorated with <code>@mcp.tool()</code>
 +
* has clearly defined inputs (parameters)
 +
* returns a result (usually a string)
 +
 +
Claude reads the function name, parameters, and docstring to decide how to use it.
 +
 +
----
 +
 +
== Try It ==
 +
 +
After your MCP server is running, test this tool in Claude Desktop:
 +
 +
<pre>
 +
Use CardinalsMCP to say hello
 +
</pre>
 +
 +
If it works, your MCP server is correctly configured.
  
 
[[Category:Module 6]]
 
[[Category:Module 6]]
 
[[Category:Modules]]
 
[[Category:Modules]]

Latest revision as of 15:55, 18 March 2026


Model Context Protocol (MCP)

Large Language Models (LLMs) like Claude, ChatGPT, and other AI assistants are powerful, but they normally operate in isolation. They cannot directly access your computer, read files, run programs, or query databases.

The Model Context Protocol (MCP) is a standard that allows language models to safely interact with external tools and data sources.

Instead of executing code directly, the language model communicates with an MCP server, which exposes specific capabilities that the model can use.

Learn more: Model Context Protocol Documentation

Mental Model: How MCP Actually Works

A helpful way to think about MCP is that the language model is a decision maker and the MCP server is a toolbox.

User asks question
      ↓
Claude decides which tool to use
      ↓
Claude calls the MCP server
      ↓
Your Python function runs
      ↓
The result is returned to Claude
      ↓
Claude explains the result to the user

Important: The language model does not run your code directly. It decides which tool should be executed. The MCP server runs the code and returns the result.

MCP Architecture

The interaction between the user, the language model, and your MCP server looks like this:

User Prompt
     ↓
Language Model (Claude)
     ↓
MCP Client
     ↓
MCP Server
     ↓
Tools / Data Sources

The language model analyzes the prompt and determines whether a tool should be used.

If a tool is needed:

1. The model selects a tool 2. The MCP client sends the request 3. The MCP server runs the function 4. The result is returned to the model 5. The model formats the final response

MCP Building Blocks

MCP servers provide three types of capabilities.

Tools

Tools are executable functions that perform actions or retrieve data.

Examples include:

  • querying a database
  • reading files
  • performing calculations
  • calling APIs
  • searching datasets

Example tool:

get_weather(city="St. Louis")

The MCP server runs the function and returns the result.

Resources

Resources are the data sources your MCP tools use.

Examples include:

  • files
  • documents
  • databases
  • logs
  • datasets

For example, a tool might read a dataset stored in a file.

Prompts

Prompts are the instructions given by the user.

Example:

Show the top hitters on the 1940 Cardinals.

The model interprets the prompt and determines if a tool should be used.

The user never runs the tool directly.

Example Workflow

Suppose the user asks:

Show the top hitters on the 1940 Cardinals.

The system behaves like this:

1. Claude reads the prompt 2. Claude decides the get_top_players tool is needed 3. Claude calls the MCP server 4. The server runs the Python function 5. The results are returned to Claude

Why MCP Matters

Without MCP, language models can only generate text.

With MCP, models can:

  • access real data
  • run programs
  • query databases
  • analyze datasets
  • automate workflows

This transforms AI systems from chatbots into agents.

MCP in This Course

In this module, you will build a simple MCP server using Python.

Your server will expose tools that allow Claude Desktop to analyze historical St. Louis Cardinals batting statistics.

Claude will automatically call your tools when needed.


Example MCP Tool: hello

Below is a simple example of an MCP tool:

@mcp.tool()
async def hello(name: str = "world") -> str:
    """Return a simple greeting to verify the server is working."""
    return f"Hello CSE 3300 Student, {name}!"

This function defines a tool that Claude Desktop can call through the MCP server.


What This Function Does

This tool returns a simple greeting message. It is primarily used to verify that:

  • your MCP server is running
  • Docker is working correctly
  • Claude Desktop is connected to your server

If this tool works, your MCP setup is likely correct.


Line-by-Line Explanation

@mcp.tool()

This is a decorator that registers the function as an MCP tool.

Without this line, Claude will not be able to see or use the function.


async def hello(...)

This defines the function.

  • hello is the tool name
  • Claude will refer to this function by this name when calling it

The keyword async indicates that the function runs within the MCP system. You do not need to fully understand asynchronous programming for this assignment.


name: str = "world"

This defines an input parameter:

  • name is the parameter name
  • str indicates it should be a string
  • "world" is the default value

If no value is provided, the function will use "world".


-> str

This is a return type hint.

It indicates that the function returns a string.

Note: Python does not strictly enforce this, but it is important for clarity.


Docstring

"""Return a simple greeting to verify the server is working."""

This is a docstring.

It serves two purposes:

  • explains the function to humans
  • helps Claude understand what the tool does and when to use it

return statement

return f"Hello CSE 3300 Student, {name}!"

This returns the output of the tool.

For example:

hello("Todd") → Hello CSE 3300 Student, Todd!

How Claude Uses This Tool

When you type a prompt such as:

Use CardinalsMCP to say hello to Todd

Claude will:

  1. recognize that the hello tool is relevant
  2. call the function with name="Todd"
  3. receive the returned string
  4. display the result in the chat

Key Takeaway

An MCP tool is just a Python function that:

  • is decorated with @mcp.tool()
  • has clearly defined inputs (parameters)
  • returns a result (usually a string)

Claude reads the function name, parameters, and docstring to decide how to use it.


Try It

After your MCP server is running, test this tool in Claude Desktop:

Use CardinalsMCP to say hello

If it works, your MCP server is correctly configured.