The Model Context Protocol (MCP) is an open standard designed to enable seamless, secure, and real-time communication between AI models and external data sources or tools. At the heart of this protocol is the MCP server, which serves as a bridge connecting AI assistants to various data repositories, APIs, files, and services, eliminating the need for cumbersome, model-specific integration code.
While MCP servers can run locally on a user’s machine, remote MCP servers are hosted on cloud or enterprise infrastructure, enabling AI models to connect over the internet to centralized data sources and services. Remote MCP servers facilitate:
- Centralized data access for multiple AI clients or users.
- Scalable resource sharing across teams or applications.
- Integration with cloud services and APIs that require persistent connections.
- Secure remote management of data access policies and permissions.
- The user provides a prompt to the Local LLM or Desktop LLM(OpenAI or Claude).
- The LLM asks the user if tools need to be invoked, and based on the user’s selection for the MCP tool, it forwards to the Remote Server.
- The MCP Server coordinates with the Database and Other Services to generate a response.
- The processed data or result is returned to the Client System for final output to the user.
What is HTTP+SSE?
In the context of the Model Context Protocol (MCP), HTTP+SSE refers to a legacy transport mechanism that uses a combination of standard HTTP requests and Server-Sent Events (SSE) to enable communication between an MCP server and AI clients.
- It allows real-time, server-to-client streaming of data, which is ideal for pushing updates or tool responses to AI clients.
- It works well with restricted networks and environments where more complex protocols like WebSockets might be blocked.
- It provides a simple and lightweight transport mechanism using standard HTTP infrastructure.
Prerequisites
- Python 3.x
- Required packages: uvicorn, yfinance, fastmcp, starlette
Setting Up the Environment
First, install the required packages:
pip install uvicorn yfinance starlette fastmcpCreate a directory and initialize uv
mkdir stockmcpcd stockmcp
uv init #creates the required folder structure. main.py can be edited for main code
The Code Structure
Our application consists of two main components:
1. Stock data retrieval functions
2. Web service setup
Stock Data Retrieval Functions
We have two main functions for fetching stock data:
1. Current Stock Price Function
@mcp.tool()def get_current_stock_price(ticker):
"""Retrieve the current stock price for a given ticker"""
try:
stock = yf.Ticker(ticker)
current_price = stock.info['regularMarketPrice']
return current_price
except Exception as e:
print(f"Error fetching current price for {ticker}: {str(e)}")
return None
This function:
- Takes a stock ticker symbol as input (e.g., ‘AAPL’, ‘MSFT’)
- Returns the current market price
- Handles errors gracefully
2. Historical Price Function
@mcp.tool()def get_historical_prices(ticker):
"""Retrieve historical stock prices for a given ticker"""
try:
stock = yf.Ticker(ticker)
hist_data = stock.history(period='1mo')
return hist_data
except Exception as e:
print(f"Error fetching historical data for {ticker}: {str(e)}")
return None
This function:
- Retrieves one month of historical price data
- Returns a pandas DataFrame with price information
- Includes daily Open, High, Low, Close, and Volume data
Web Service Setup
The application uses Starlette (a lightweight ASGI framework) to create a web service.
app = Starlette(routes=[
Mount("/", app=mcp.sse_app()),
]
)
The server runs on localhost (127.0.0.1) on port 8000
if __name__ == "__main__":uvicorn.run(app, host="127.0.0.1", port=8000)
Complete Code
import uvicornimport yfinance as yf
from datetime import datetime, timedelta
from mcp.server.fastmcp import FastMCP
from starlette.applications import Starlette
from starlette.routing import Mount
mcp = FastMCP("Stocks")
@mcp.tool()
def get_current_stock_price(ticker):
"""
Retrieve the current stock price for a given ticker
Args:
ticker (str): Stock ticker symbol (e.g., 'AAPL', 'MSFT')
Returns:
float: Current stock price
"""
try:
stock = yf.Ticker(ticker)
current_price = stock.info['regularMarketPrice']
return current_price
except Exception as e:
print(f"Error fetching current price for {ticker}: {str(e)}")
return None
@mcp.tool()
def get_historical_prices(ticker):
"""
Retrieve historical stock prices for a given ticker
Args:
ticker (str): Stock ticker symbol (e.g., 'AAPL', 'MSFT')
Returns:
pandas.DataFrame: Historical price data
"""
try:
stock = yf.Ticker(ticker)
hist_data = stock.history(period='1mo')
return hist_data
except Exception as e:
print(f"Error fetching historical data for {ticker}: {str(e)}")
return None
app = Starlette(
routes=[
Mount("/", app=mcp.sse_app()),
]
)
if __name__ == "__main__":
uvicorn.run(app, host="127.0.0.1", port=8000)
Using the Application
To use this application:
- Run the script using Python:
2. The server will start on http://127.0.0.1:8000
Testing tools with @modelcontextprotocol/inspector
Run the following command in another shell
This brings up the MCP inspector. It is an interactive developer tool for testing and debugging MCP servers.
We can see in the tools tab, our tools are displayed.
Now that the MCP server is up and running. We can start our Claude Desktop app. Claude Desktop provides a utility to add tools and use them.
Add the new MCP server as follows in the claude_desktop_config.json
Restart the Claude Desktop App. You should see new tools in settings
Let us ask Claude what the stock price is for Microsoft.
And we get the response as intended.
Next, let us ask historical stock prices for AAPL in table format.
This is a simple MCP server app with the ability to run remotely, which has its advantages. Yet we are running it on localhost.
There is a lot of room for improvement. Trying to run the MCP tools on another machine is what truly makes it remote.
We can also create MCP servers that perform other activities like fetching results from the DB or connecting to services.
Check out the various MCPs available to start with, and then plan to create your own.
.png)

