File size: 10,189 Bytes
719b2f9
 
 
 
 
 
 
 
55141bb
719b2f9
 
 
 
af6d09a
 
 
719b2f9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
af6d09a
719b2f9
 
 
 
 
 
 
 
af6d09a
 
 
 
 
 
 
 
 
 
 
719b2f9
 
 
af6d09a
719b2f9
 
 
 
af6d09a
 
 
 
 
 
719b2f9
 
 
 
 
 
 
 
 
 
af6d09a
719b2f9
 
 
 
 
ee65abb
719b2f9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ee65abb
 
 
 
 
 
 
 
 
 
 
 
 
 
aee78d5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ee65abb
 
 
 
 
 
 
 
 
 
 
719b2f9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ee65abb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
719b2f9
 
 
 
 
 
 
 
 
 
ee65abb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
719b2f9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# FoodWise MCP Server

## Overview

The FoodWise MCP (Model Context Protocol) server provides AI agents with comprehensive inventory management capabilities. It exposes food inventory data and operations through a standardized protocol that enables intelligent assistance with meal planning, expiration management, and kitchen organization.

## Architecture

The MCP server is built using [FastMCP](https://github.com/jlowin/fastmcp) and organized into three focused modules:

```
src/foodwise/mcp_server/
β”œβ”€β”€ main.py                # Entry point & primitive registration  
β”œβ”€β”€ fastmcp_tools.py       # πŸ”§ CRUD operations (12 tools)
β”œβ”€β”€ fastmcp_resources.py   # πŸ“‚ Live data sources (4 resources)
β”œβ”€β”€ fastmcp_prompts.py     # πŸ“ Workflow templates (5 prompts)
└── README.md              # This documentation
```

### Design Principles

- **Separation of Concerns**: Each module handles one type of MCP primitive
- **Live Data**: Resources always fetch fresh data from Notion (no caching)
- **Defensive Programming**: Graceful handling of missing/invalid Notion data
- **Type Safety**: Full type annotations for better maintainability

## MCP Primitives

### πŸ”§ Tools (Actions)
*Defined in `fastmcp_tools.py`*

**Inventory Tools (5):**
| Tool | Purpose | Key Parameters |
|------|---------|----------------|
| `get_inventory` | Retrieve inventory items with filtering | `filter_type`, `category`, `storage_type`, `expiring_days` |
| `add_inventory_item` | Add new food items | `name`, `category`, `storage_type`, `quantity`, `unit` + optional fields |
| `search_inventory_items` | Find items by name/category | `search_term`, `search_type` |
| `update_inventory_item` | Modify existing items | `page_id`, `updated_data` |
| `remove_inventory_item` | Archive items | `page_id` |

**Shopping Tools (7):**
| Tool | Purpose | Key Parameters |
|------|---------|----------------|
| `get_shopping_list` | Retrieve shopping items with filtering | `store`, `priority`, `status` |
| `add_shopping_item` | Add new shopping items | `item_name`, `quantity`, `unit`, `store`, `priority` + optional fields |
| `update_shopping_item` | Modify shopping items | `page_id` + update fields |
| `remove_shopping_item` | Archive shopping items | `page_id` |
| `create_shopping_from_recipe` | Generate shopping list from recipe | `recipe_ingredients`, `servings`, `preferred_store` |
| `optimize_shopping_by_store` | Organize shopping by store | Returns store-grouped lists with tips |
| `move_purchased_to_inventory` | Move purchased items to inventory | `purchased_item_ids` |

### πŸ“‚ Resources (Live Data)
*Defined in `fastmcp_resources.py`*

**Inventory Resources (2):**
| Resource URI | Purpose | Data Provided |
|--------------|---------|---------------|
| `inventory://items` | Complete inventory state | All items with metadata |
| `inventory://expiring-items` | Expiration alerts | Items expiring in next 7 days (urgent/moderate/upcoming) |

**Shopping Resources (2):**
| Resource URI | Purpose | Data Provided |
|--------------|---------|---------------|
| `shopping://list` | Current shopping list | All shopping items organized by status |
| `shopping://by-store` | Shopping organized by store | Items grouped by preferred store with totals |

### πŸ“ Prompts (Workflow Templates)
*Defined in `fastmcp_prompts.py`*

| Prompt | Purpose | Key Features |
|--------|---------|-------------|
| `add_item_helper` | Guide item entry | Required/optional fields, validation, organization tips |
| `meal_planning_assistant` | Plan meals using available inventory | Prioritize expiring items, provide recipes, shopping lists |
| `expiration_manager` | Handle items expiring soon | Safety assessment, usage strategies, preservation methods |
| `shopping_list_optimizer` | Create smart shopping lists | Avoid duplicates, consider storage capacity |
| `smart_shopping_assistant` | General shopping guidance | Budget management, store optimization, duplicate prevention |

## Integration

### Claude Desktop Configuration

#### Local MCP Server
Add to your `claude_desktop_config.json`:

```json
{
  "mcpServers": {
    "foodwise": {
      "command": "uv",
      "args": ["run", "python", "-m", "foodwise.mcp_server"],
      "cwd": "/path/to/FoodWise",
      "env": {
        "MCP_LOG_LEVEL": "INFO"
      }
    }
  }
}
```

For development with debug logging:

```json
{
  "mcpServers": {
    "foodwise-dev": {
      "command": "uv", 
      "args": ["run", "fastmcp", "dev", "src/foodwise/mcp_server/main.py"],
      "cwd": "/path/to/FoodWise",
      "env": {
        "MCP_LOG_LEVEL": "DEBUG"
      }
    }
  }
}
```

#### Remote MCP Server βœ… NEW!
For remote access without local setup:

```json
{
  "mcpServers": {
    "foodwise-remote": {
      "command": "npx",
      "args": ["supergateway", "--streamableHttp", "https://leowalker-foodwise-remote-mcp.hf.space/mcp/"]
    }
  }
}
```

### Remote MCP Server with Token (Recommended)
Enable a lightweight token gate compatible with Desktop + `supergateway`.

```json
{
  "mcpServers": {
    "foodwise-remote": {
      "command": "npx",
      "args": [
        "supergateway",
        "--streamableHttp",
        "https://<your-space>.hf.space/mcp/?key=$MCP_AUTH_TOKEN"
      ]
    }
  }
}
```

Notes:
- Prefer the `Authorization: Bearer` header in general; `?key=` is provided for bridge compatibility.
- When `MCP_AUTH_TOKEN` is set, `main.py` runs MCP upstream on `127.0.0.1:7870` and the proxy on public `PORT` (7860).
- Health check (no auth): `GET /health` β†’ `{ "status": "ok" }`.

**🌐 Remote Deployment**: The MCP server is deployed on HuggingFace Spaces using Docker with:
- FastMCP HTTP transport
- Python 3.12 + uv package management
- Automatic OAuth-free access for MCP clients
- Environment secrets managed in HuggingFace Space settings

**Requirements for Remote Access**:
- `npx` installed (comes with Node.js)
- `supergateway` package (automatically installed via npx)
- Internet connection

### Usage Examples

**Get expiring items:**
```
Use the inventory://expiring-items resource to see what needs attention
```

**Plan meals:**
```
Use the meal_planning_assistant prompt with current inventory data
```

**Add new item:**
```
Use add_inventory_item tool with: milk, Dairy, Refrigerator, 1, gallon
```

## Data Backend

The MCP server connects to a **Notion database** that stores all inventory data. The `FoodWiseNotionClient` handles:

- βœ… CRUD operations on inventory items
- βœ… Filtering and search functionality  
- βœ… Expiration date calculations
- βœ… Defensive handling of flexible Notion schema

### Database Schema

Key Notion properties:
- **Name** (title): Item identifier
- **Category** (select): Food category (Dairy, Raw Meat, etc.)
- **Storage Type** (select): Pantry/Refrigerator/Freezer
- **Quantity** (number): Amount available
- **Unit** (text): Measurement unit
- **Best By Date** (date): Expiration date
- **Location/Shelf** (text): Specific storage location
- **Tags** (multi-select): Organization labels

## Error Handling

The server implements comprehensive error handling:

- **Notion API errors**: Graceful degradation with error messages
- **Missing data**: Defensive programming with sensible defaults
- **Type mismatches**: Safe conversion with fallbacks
- **Network issues**: Timeout handling and retry logic

## Development

### Running the Server

```bash
# Basic usage (stdio transport)
uv run python -m foodwise.mcp_server

# With debug logging
MCP_LOG_LEVEL=DEBUG uv run python -m foodwise.mcp_server

# Using FastMCP CLI with HTTP transport
uv run fastmcp run src/foodwise/mcp_server/main.py --transport http --port 8080

# Custom server name
MCP_SERVER_NAME="MyFoodWise" uv run python -m foodwise.mcp_server
```

#### Remote Deployment (HTTP Mode)
For HuggingFace Spaces or similar cloud deployment:

```bash
# Using the main.py entry point (for Docker/cloud deployment)
uv run python main.py

# With custom environment
HOST=0.0.0.0 PORT=7860 ENVIRONMENT=production uv run python main.py

# Local HTTP testing (before deployment)
HOST=127.0.0.1 PORT=7860 uv run python main.py
```

**Remote Server Features**:
- HTTP transport for web accessibility
- Environment-based configuration
- Docker containerization ready
- OAuth-free MCP client access

### Testing Integration

```bash
# Test with manual MCP client
uv run python scripts/manual_test.py

# Test with FastMCP dev server (includes MCP Inspector)
uv run fastmcp dev src/foodwise/mcp_server/main.py
```

#### Remote Server Testing
```bash
# Test remote MCP server with MCP Inspector
npx @modelcontextprotocol/inspector "https://leowalker-foodwise-remote-mcp.hf.space/mcp/"

# Test supergateway bridge locally
npx supergateway --streamableHttp "https://leowalker-foodwise-remote-mcp.hf.space/mcp/" --logLevel debug

# Verify remote server health
curl -I "https://leowalker-foodwise-remote-mcp.hf.space/mcp/"
```

**Remote Testing Notes**:
- MCP Inspector provides interactive testing interface
- `supergateway` bridges HTTP MCP to stdio for Claude Desktop
- Remote server responds on `/mcp/` endpoint with Streamable-HTTP protocol

### Adding New Primitives

1. **Tools**: Add to `fastmcp_tools.py` and register in `register_inventory_tools()`
2. **Resources**: Add to `fastmcp_resources.py` and register in `register_resources()`  
3. **Prompts**: Add template constant and function to `fastmcp_prompts.py`

## Configuration

The server follows FastMCP best practices for minimal configuration:

- **Environment Variables**: 
  - `MCP_SERVER_NAME` - Override server name (default: "FoodWise")
  - `MCP_LOG_LEVEL` - Set logging level (DEBUG, INFO, WARNING, ERROR)
- **CLI Arguments**: Use `fastmcp run` with transport and port options
- **Zero Config**: Works out of the box with sensible defaults

## Future Enhancements

- [ ] External recipe API integration
- [ ] Configurable meal suggestion algorithms  
- [ ] Batch operations for bulk inventory updates
- [ ] Advanced filtering and sorting options
- [ ] Integration with grocery delivery APIs
- [ ] Nutritional information tracking

---

*For more information about the broader FoodWise project, see the main [README.md](../../../README.md).*