Difference between revisions of "MCP Fundamentals"

From CSE330 Wiki
Jump to navigationJump to search
(Created page with "__NOTOC__ = Model Context Protocol (MCP) = Large Language Models (LLMs) like Claude, ChatGPT, and other AI assistants are powerful, but they normally operate in isolation....")
 
 
(One intermediate revision by the same user not shown)
Line 9: Line 9:
  
 
Instead of executing code directly, the language model communicates with an '''MCP server''', which exposes specific capabilities that the model can use.
 
Instead of executing code directly, the language model communicates with an '''MCP server''', which exposes specific capabilities that the model can use.
−
 
−
This enables AI systems to interact with real software and data.
 
  
 
Learn more:   
 
Learn more:   
 
[https://modelcontextprotocol.io/docs/getting-started/intro Model Context Protocol Documentation]
 
[https://modelcontextprotocol.io/docs/getting-started/intro 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'''.
 +
 +
<pre>
 +
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
 +
</pre>
 +
 +
'''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 ==
 
== MCP Architecture ==
Line 31: Line 51:
 
</pre>
 
</pre>
  
−
The language model analyzes the user prompt and determines whether a tool should be used.
+
The language model analyzes the prompt and determines whether a tool should be used.
  
 
If a tool is needed:
 
If a tool is needed:
  
−
1. The language model selects the appropriate tool.
+
1. The model selects a tool
−
2. The MCP client sends the request to the MCP server.
+
2. The MCP client sends the request
−
3. The server executes the tool.
+
3. The MCP server runs the function
−
4. The result is returned to the model.
+
4. The result is returned to the model
−
5. The model formats the response for the user.
+
5. The model formats the final response
−
 
 
−
The model never directly runs code on your computer.
 
  
 
== MCP Building Blocks ==
 
== MCP Building Blocks ==
Line 58: Line 76:
 
* calling APIs
 
* calling APIs
 
* searching datasets
 
* searching datasets
−
 
−
A language model can decide to call one of these tools to answer a user's request.
 
  
 
Example tool:
 
Example tool:
Line 67: Line 83:
 
</pre>
 
</pre>
  
−
The MCP server runs the function and returns the result to the language model.
+
The MCP server runs the function and returns the result.
  
 
=== Resources ===
 
=== Resources ===
  
−
'''Resources''' are data sources that the MCP server can access.
+
'''Resources''' are the data sources your MCP tools use.
  
 
Examples include:
 
Examples include:
  
 
* files
 
* files
 +
* documents
 
* databases
 
* databases
−
* documents
+
* logs
 
* datasets
 
* datasets
−
* logs
 
−
 
−
Resources provide context for the tools.
 
  
 
For example, a tool might read a dataset stored in a file.
 
For example, a tool might read a dataset stored in a file.
Line 92: Line 106:
  
 
<pre>
 
<pre>
−
"Show the top hitters on the 1940 Cardinals."
+
Show the top hitters on the 1940 Cardinals.
 
</pre>
 
</pre>
  
−
The language model interprets the prompt and decides whether it needs to call a tool.
+
The model interprets the prompt and determines if a tool should be used.
  
−
The user never directly runs the tool — the model decides when tools are needed.
+
The user never runs the tool directly.
  
 
== Example Workflow ==
 
== Example Workflow ==
Line 104: Line 118:
  
 
<pre>
 
<pre>
−
"Show the top hitters on the 1940 Cardinals."
+
Show the top hitters on the 1940 Cardinals.
 
</pre>
 
</pre>
  
−
The system might behave like this:
+
The system behaves like this:
−
 
 
−
1. Claude reads the prompt.
 
−
2. Claude determines that the request requires the tool <code>get_top_players</code>.
 
−
3. Claude calls the MCP server.
 
−
4. The server runs the Python function.
 
−
5. The result is returned and displayed to the user.
 
  
−
This process happens automatically.
+
1. Claude reads the prompt
 +
2. Claude decides the <code>get_top_players</code> 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 ==
 
== Why MCP Matters ==
Line 121: Line 133:
 
Without MCP, language models can only generate text.
 
Without MCP, language models can only generate text.
  
−
With MCP, language models can:
+
With MCP, models can:
  
 
* access real data
 
* access real data
Line 129: Line 141:
 
* automate workflows
 
* automate workflows
  
−
This transforms AI systems from simple chatbots into '''agents''' capable of performing real tasks.
+
This transforms AI systems from chatbots into '''agents'''.
  
 
== MCP in This Course ==
 
== MCP in This Course ==
Line 137: Line 149:
 
Your server will expose tools that allow Claude Desktop to analyze historical St. Louis Cardinals batting statistics.
 
Your server will expose tools that allow Claude Desktop to analyze historical St. Louis Cardinals batting statistics.
  
−
Claude will automatically call your tools when appropriate and display the results in the chat interface.
+
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.