Kroger MCP Server
This project implements a Model Context Protocol (MCP) server that wraps the Kroger API. It allows Large Language Models (LLMs) like Anthropic's Claude to interact with Kroger's grocery services, enabling features such as product search, store lookup, and cart management.
Features
- OAuth2 Authentication: Handles client credentials for general data and user-based authorization for cart operations.
- Product Search: Search for products by keyword at specific store locations.
- Product Details: Retrieve detailed product information including price, availability, and fulfillment options.
- Store Locations: Find Kroger store locations by ZIP code.
- Cart Management: Add items to a user's Kroger shopping cart (requires user authorization).
1. Configuration
Before running the server, you need to configure your Kroger API credentials and OAuth2 settings.
1.1. API Credentials (Client ID & Secret)
- Obtain Credentials: Register an application on the Kroger Developer Portal to get a
Client ID
andClient Secret
. - Set Credentials: You can set these credentials in one of two ways:
config.py
(Recommended for simplicity for local use): Open theconfig.py
file and replace the placeholder values forKROGER_CLIENT_ID
andKROGER_CLIENT_SECRET
with your actual credentials.- Environment Variables: The
auth.py
script can be modified to also reados.environ.get("KROGER_CLIENT_ID")
andos.environ.get("KROGER_CLIENT_SECRET")
if you prefer managing secrets via environment variables. (Note: Current implementation intools.py
andserver.py
directly usesconfig.py
values).
1.2. User Authorization (for Cart Operations)
To use tools that modify a user's cart (e.g., add_to_cart
), the user must authorize the application. This server uses the OAuth2 Authorization Code Grant flow.
- Redirect URI: Ensure the
KROGER_REDIRECT_URI
inconfig.py
matches the Redirect URI registered with your Kroger application. For local testing,http://localhost:8080/callback
is a common default, but you'll need a way to capture the code from this redirect. - Obtaining an Authorization Code & Refresh Token:
- Run the
auth.py
script directly (python auth.py
). - It will print an "Authorization URL". Copy and paste this URL into your web browser.
- Log in with your Kroger account and grant access.
- You will be redirected to your
KROGER_REDIRECT_URI
. The URL in your browser's address bar will now contain an authorizationcode
(e.g.,http://localhost:8080/callback?code=YOUR_AUTH_CODE&...
). - Copy this
code
. - Paste the
code
back into theauth.py
script when prompted. - The script will then exchange the code for an access token and a refresh token.
- Crucially, securely store the displayed
refresh_token
.
- Run the
- Configuring the Refresh Token:
- To enable cart operations across server restarts without re-authenticating each time, you should set the obtained
refresh_token
inconfig.py
or as an environment variable thatAuthManager
can load. - Modify
AuthManager.__init__
inauth.py
to load thisKROGER_USER_REFRESH_TOKEN
fromconfig.py
or environment:And then addKROGER_USER_REFRESH_TOKEN = "YOUR_SAVED_REFRESH_TOKEN"
toconfig.py
. - When
get_user_token()
is called, if an access token is expired or missing, it will attempt to use this refresh token.
- To enable cart operations across server restarts without re-authenticating each time, you should set the obtained
2. Running the Server
- Install Dependencies: If you haven't already, install the required Python libraries:(Note: The
mcp
library name is assumed; adjust if it's different, e.g.,modelcontextprotocol
). - Start the Server:
Run the
server.py
script from your terminal: - Server Operation:
- The server uses STDIO (standard input/output) for communication with the MCP client. It does not open any network ports.
- Upon starting, it will print initialization messages, including a list of registered tools.
- It will then listen for JSON-RPC requests from the MCP client.
- Stopping the Server:
Press
Ctrl+C
in the terminal where the server is running.
3. MCP Client Integration
3.1. Claude Desktop
- Go to
Settings
in Claude Desktop. - Navigate to
Integrations
(or a similar section for MCP servers). - Click
Add MCP Server
(or equivalent). - Provide the command to run the server. This usually involves specifying the Python interpreter and the path to
server.py
. For example:- If Python is in your PATH:
python /path/to/your/project/server.py
- Otherwise:
/path/to/your/python /path/to/your/project/server.py
- If Python is in your PATH:
- Once added, Claude will be able to see and invoke the Kroger tools (e.g.,
find_stores
,search_products
).
3.2. Programmatic Use (Example)
Developers can also interact with the server programmatically using an MCP client library.
4. Example Dialogue with LLM
User: "I need two gallons of organic whole milk and a dozen eggs from a Kroger near 90210."
LLM (Assistant) Internal Steps:
- (Optional: LLM asks for user's ZIP code if not provided or ambiguous)
- LLM calls
find_stores
:{"zip_code": "90210", "limit": 1}
- Server returns store details, e.g.,
[{ "locationId": "01400123", "name": "Beverly Hills Kroger", ... }]
- Server returns store details, e.g.,
- LLM calls
search_products
(for milk):{"query": "organic whole milk", "location_id": "01400123", "limit": 5}
- Server returns list of milk products. LLM selects one, e.g.,
{"productId": "0001111060404", "description": "Simple Truth Organic Milk...", ...}
.
- Server returns list of milk products. LLM selects one, e.g.,
- LLM calls
search_products
(for eggs):{"query": "dozen eggs", "location_id": "01400123", "limit": 3}
- Server returns list of egg products. LLM selects one.
- (User authorization for cart must be completed if not done already)
- LLM calls
add_to_cart
(for milk):{"product_id": "0001111060404", "quantity": 2, "location_id": "01400123"}
- Server confirms addition.
- LLM calls
add_to_cart
(for eggs):{"product_id": "...", "quantity": 1, "location_id": "01400123"}
- Server confirms addition.
LLM (Assistant) to User: "Okay, I've found the Beverly Hills Kroger. I've added 2 gallons of Simple Truth Organic Whole Milk and one dozen eggs to your cart. Anything else?"
5. Error Scenarios
- Missing User Authorization: If you attempt to use
add_to_cart
without the user having authorized the application, the tool will return an error:The LLM should guide the user to perform the authorization step (see Section 1.2). The authorization URL might be included in the error message. - Invalid/Expired Tokens: If an access token is expired, the
AuthManager
will attempt to refresh it. If the refresh token is also invalid (e.g., for user tokens after a 401 error onadd_to_cart
), re-authorization will be required. - API Rate Limits: Kroger's API has rate limits (e.g., see developer.kroger.com/support/rate-limits/). If the server hits these limits, API calls will fail. The server will return an error from Kroger, typically with an HTTP 429 status code. The LLM should inform the user to try again later.
- Other API Errors: If Kroger's API returns other errors (e.g., invalid product ID, store not found for locationId), the tools will return a JSON dictionary containing
error
,details
,status_code
(the HTTP status from Kroger), and possiblyraw_response
orkroger_error
fields.
6. Available Tools
The server exposes the following tools to the LLM:
find_stores(zip_code: str, radius_miles: int = 10, limit: int = 5) -> list | dict
- Description: Find Kroger store locations by ZIP code (returns nearest stores with IDs).
search_products(query: str, location_id: str, limit: int = 10) -> list | dict
- Description: Search Kroger products by keyword at a given store.
get_product(product_id: str, location_id: str) -> dict
- Description: Get detailed information for a product by ID (price, size, stock, fulfillment options).
add_to_cart(product_id: str, quantity: int, location_id: str) -> dict
- Description: Add a product to the user's Kroger cart (requires user authentication).
(The descriptions above are based on the @tool
decorators in tools.py
.)
This server cannot be installed
Allows Large Language Models to interact with Kroger's grocery services, enabling product search, store lookup, and cart management through the Model Context Protocol.
Related MCP Servers
- -securityAlicense-qualityA Model Context Protocol server that enables Large Language Models to seamlessly interact with ClickHouse databases, supporting resource listing, schema retrieval, and query execution.Last updated -1PythonMIT License
- -securityAlicense-qualityA Model Context Protocol server that enables Large Language Models to interact with ClickUp workspace tasks and data, allowing creation and retrieval of tasks through natural language.Last updated -59TypeScriptMIT License
MCP TapData Serverofficial
-securityFlicense-qualityA Model Context Protocol server that enables Large Language Models to access and interact with database connections, including viewing schemas and performing CRUD operations on connected databases.Last updated -- -securityAlicense-qualityA Model Context Protocol server that enables large language models like Claude to perform comprehensive interactions with Firebase Firestore databases, supporting full CRUD operations, complex queries, and advanced features like transactions and TTL management.Last updated -482JavaScriptMIT License