nutritionix-mcp 1.0.0 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +19 -0
- package/dist/api.js +3 -0
- package/dist/gateway.d.ts +16 -0
- package/dist/gateway.js +85 -0
- package/dist/server.js +39 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -26,3 +26,22 @@ The server uses stdio, so it can be connected to Claude Desktop, Cursor, VS Code
|
|
|
26
26
|
## Limits and privacy
|
|
27
27
|
|
|
28
28
|
This project is intentionally narrow. It should be treated as a practical helper, not a complete certification or security audit. Check the implementation and the returned data before using it with sensitive material. Keys stay local and go only to Nutritionix. Read-only.
|
|
29
|
+
|
|
30
|
+
<!-- paywall -->
|
|
31
|
+
## Premium tools
|
|
32
|
+
|
|
33
|
+
These tools need a license key:
|
|
34
|
+
|
|
35
|
+
* `nutrient_food_search`
|
|
36
|
+
* `nutrient_food_profile`
|
|
37
|
+
|
|
38
|
+
Buy a key at https://mcp-marketplace.io/server/io-github-mrfentmen-nutritionix-mcp and set it in your MCP client config:
|
|
39
|
+
|
|
40
|
+
```json
|
|
41
|
+
"env": { "MCP_LICENSE_KEY": "mcp_live_..." }
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
The key is checked against MCP Marketplace, cached for 24 hours, and keeps
|
|
45
|
+
working offline once one check has succeeded. Every other tool on this server
|
|
46
|
+
stays free.
|
|
47
|
+
<!-- /paywall -->
|
package/dist/api.js
CHANGED
|
@@ -2,6 +2,9 @@
|
|
|
2
2
|
* Nutritionix API v2 client. Needs NUTRITIONIX_APP_ID + NUTRITIONIX_API_KEY
|
|
3
3
|
* (free at https://developer.nutritionix.com/).
|
|
4
4
|
* Docs: https://developer.nutritionix.com/docs/v2
|
|
5
|
+
*
|
|
6
|
+
* The USDA FoodData Central premium tools moved to the licence gateway
|
|
7
|
+
* (src/gateway.ts); what remains here is the free Nutritionix client.
|
|
5
8
|
*/
|
|
6
9
|
const BASE = "https://trackapi.nutritionix.com/v2";
|
|
7
10
|
export class NutritionixError extends Error {
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Thin client for the premium gateway.
|
|
3
|
+
*
|
|
4
|
+
* The premium tools do not run in this package any more. Their bodies live on
|
|
5
|
+
* the gateway, which checks a Polar licence key before it does any work, so
|
|
6
|
+
* deleting anything here buys nothing: the answer only exists behind the gate.
|
|
7
|
+
*
|
|
8
|
+
* Set `MCP_LICENSE_KEY` to a Polar licence key. `MCP_GATEWAY_URL` overrides the
|
|
9
|
+
* endpoint, which is only useful for testing.
|
|
10
|
+
*/
|
|
11
|
+
/** Raised when a premium call cannot be completed. `message` is safe to show. */
|
|
12
|
+
export declare class GatewayError extends Error {
|
|
13
|
+
readonly refused: boolean;
|
|
14
|
+
constructor(message: string, refused: boolean);
|
|
15
|
+
}
|
|
16
|
+
export declare function premiumTool(tool: string, args: Record<string, unknown>): Promise<string>;
|
package/dist/gateway.js
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Thin client for the premium gateway.
|
|
3
|
+
*
|
|
4
|
+
* The premium tools do not run in this package any more. Their bodies live on
|
|
5
|
+
* the gateway, which checks a Polar licence key before it does any work, so
|
|
6
|
+
* deleting anything here buys nothing: the answer only exists behind the gate.
|
|
7
|
+
*
|
|
8
|
+
* Set `MCP_LICENSE_KEY` to a Polar licence key. `MCP_GATEWAY_URL` overrides the
|
|
9
|
+
* endpoint, which is only useful for testing.
|
|
10
|
+
*/
|
|
11
|
+
const DEFAULT_GATEWAY_URL = 'https://mcp-gateway.contactae2000.workers.dev/mcp';
|
|
12
|
+
const DEFAULT_TIMEOUT_MS = 25000;
|
|
13
|
+
const USER_AGENT = 'mrfentmen-mcp/1.0';
|
|
14
|
+
/** Raised when a premium call cannot be completed. `message` is safe to show. */
|
|
15
|
+
export class GatewayError extends Error {
|
|
16
|
+
refused;
|
|
17
|
+
constructor(message, refused) {
|
|
18
|
+
super(message);
|
|
19
|
+
this.refused = refused;
|
|
20
|
+
this.name = 'GatewayError';
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
export async function premiumTool(tool, args) {
|
|
24
|
+
const key = (process.env.MCP_LICENSE_KEY ?? '').trim();
|
|
25
|
+
if (!key) {
|
|
26
|
+
throw new GatewayError(`"${tool}" is a premium tool and no licence key is set. Set MCP_LICENSE_KEY in your MCP client config.`, true);
|
|
27
|
+
}
|
|
28
|
+
const url = (process.env.MCP_GATEWAY_URL ?? DEFAULT_GATEWAY_URL).trim();
|
|
29
|
+
const controller = new AbortController();
|
|
30
|
+
const timer = setTimeout(() => controller.abort(), DEFAULT_TIMEOUT_MS);
|
|
31
|
+
let response;
|
|
32
|
+
try {
|
|
33
|
+
response = await fetch(url, {
|
|
34
|
+
method: 'POST',
|
|
35
|
+
headers: {
|
|
36
|
+
'content-type': 'application/json',
|
|
37
|
+
accept: 'application/json',
|
|
38
|
+
'user-agent': USER_AGENT,
|
|
39
|
+
authorization: `Bearer ${key}`,
|
|
40
|
+
},
|
|
41
|
+
body: JSON.stringify({
|
|
42
|
+
jsonrpc: '2.0',
|
|
43
|
+
id: 1,
|
|
44
|
+
method: 'tools/call',
|
|
45
|
+
params: { name: tool, arguments: args },
|
|
46
|
+
}),
|
|
47
|
+
signal: controller.signal,
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
catch {
|
|
51
|
+
throw new GatewayError(`"${tool}" could not reach the licence gateway. Check your connection and try again.`, false);
|
|
52
|
+
}
|
|
53
|
+
finally {
|
|
54
|
+
clearTimeout(timer);
|
|
55
|
+
}
|
|
56
|
+
if (!response.ok) {
|
|
57
|
+
throw new GatewayError(`"${tool}" could not reach the licence gateway (HTTP ${response.status}).`, false);
|
|
58
|
+
}
|
|
59
|
+
let body;
|
|
60
|
+
try {
|
|
61
|
+
body = (await response.json());
|
|
62
|
+
}
|
|
63
|
+
catch {
|
|
64
|
+
throw new GatewayError(`"${tool}" got an unreadable answer from the licence gateway.`, false);
|
|
65
|
+
}
|
|
66
|
+
if (body.error) {
|
|
67
|
+
throw new GatewayError(`"${tool}" was refused by the licence gateway: ${body.error.message ?? 'unknown error'}`, false);
|
|
68
|
+
}
|
|
69
|
+
const result = body.result ?? {};
|
|
70
|
+
const text = (result.content ?? [])
|
|
71
|
+
.map((part) => part.text ?? '')
|
|
72
|
+
.join('\n')
|
|
73
|
+
.trim();
|
|
74
|
+
if (result.isError) {
|
|
75
|
+
// The gateway marks a licence refusal structurally, so a missing or rejected
|
|
76
|
+
// key is told apart from a tool that failed after the gate let it through.
|
|
77
|
+
if (result.structuredContent?.licenceRefusal) {
|
|
78
|
+
throw new GatewayError(text || `"${tool}" needs a valid licence key.`, true);
|
|
79
|
+
}
|
|
80
|
+
throw new GatewayError(text || `"${tool}" failed on the licence gateway.`, false);
|
|
81
|
+
}
|
|
82
|
+
if (!text)
|
|
83
|
+
throw new GatewayError(`"${tool}" returned no data.`, false);
|
|
84
|
+
return text;
|
|
85
|
+
}
|
package/dist/server.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { premiumTool } from "./gateway.js";
|
|
1
2
|
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
2
3
|
import { z } from "zod";
|
|
3
4
|
import { analyzeMeal, formatFood, NutritionixError, searchFoods } from "./api.js";
|
|
@@ -47,6 +48,44 @@ export function createServer() {
|
|
|
47
48
|
return textError(errorMessage(e));
|
|
48
49
|
}
|
|
49
50
|
});
|
|
51
|
+
server.registerTool("nutrient_food_search", {
|
|
52
|
+
title: "Search USDA foods",
|
|
53
|
+
description: "Search USDA FoodData Central for a food and get its real per-100 g energy, macros, fibre, sugars, sodium, calcium and iron.",
|
|
54
|
+
inputSchema: z.object({
|
|
55
|
+
query: z.string().describe("Food name, e.g. 'oat milk', 'cheddar cheese'"),
|
|
56
|
+
limit: z.number().int().min(1).max(20).default(5),
|
|
57
|
+
branded: z.boolean().default(false).describe("Also search packaged branded food, whose nutrient data is often missing"),
|
|
58
|
+
}),
|
|
59
|
+
annotations: READ_ONLY,
|
|
60
|
+
}, async (args) => {
|
|
61
|
+
// ---- paywall: nutrient_food_search ----
|
|
62
|
+
// The body runs on the gateway, behind the licence gate.
|
|
63
|
+
// ---- paywall: end ----
|
|
64
|
+
try {
|
|
65
|
+
return text(await premiumTool("mcp_nutritionix_nutrient_food_search", args));
|
|
66
|
+
}
|
|
67
|
+
catch (e) {
|
|
68
|
+
return textError(errorMessage(e));
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
server.registerTool("nutrient_food_profile", {
|
|
72
|
+
title: "USDA food nutrient profile",
|
|
73
|
+
description: "Full nutrient profile for one FoodData Central food by FDC id: macros, sugars, fibre, sodium, calcium, iron plus vitamins and minerals.",
|
|
74
|
+
inputSchema: z.object({
|
|
75
|
+
fdcId: z.string().describe("Numeric FDC id from nutrient_food_search, e.g. 2257046"),
|
|
76
|
+
}),
|
|
77
|
+
annotations: READ_ONLY,
|
|
78
|
+
}, async (args) => {
|
|
79
|
+
// ---- paywall: nutrient_food_profile ----
|
|
80
|
+
// The body runs on the gateway, behind the licence gate.
|
|
81
|
+
// ---- paywall: end ----
|
|
82
|
+
try {
|
|
83
|
+
return text(await premiumTool("mcp_nutritionix_nutrient_food_profile", args));
|
|
84
|
+
}
|
|
85
|
+
catch (e) {
|
|
86
|
+
return textError(errorMessage(e));
|
|
87
|
+
}
|
|
88
|
+
});
|
|
50
89
|
return server;
|
|
51
90
|
}
|
|
52
91
|
function errorMessage(e) {
|
package/package.json
CHANGED