Skip to content

Commit f1abac4

Browse files
committed
Formatting code
1 parent 946e69a commit f1abac4

File tree

1 file changed

+81
-24
lines changed

1 file changed

+81
-24
lines changed

src/routes/contextRoutes.ts

Lines changed: 81 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,109 @@
1-
import { FastifyInstance } from 'fastify';
1+
import { FastifyInstance } from "fastify";
2+
import cors from "@fastify/cors";
3+
import axios from "axios";
24

35
const name = process.env.MCP_NAME || "MCP Service";
4-
const description = process.env.MCP_DESCRIPTION || "MCP Server providing model context.";
5-
const tags = (process.env.MCP_TAGS || "MCP").split(',');
6+
const description =
7+
process.env.MCP_DESCRIPTION || "MCP Server providing model context.";
8+
const tags = (process.env.MCP_TAGS || "MCP").split(",");
69
const email = process.env.MCP_CONTACT_EMAIL || "[email protected]";
710
const website = process.env.MCP_CONTACT_WEBSITE || "https://ph7.me";
8-
const version = process.env.APP_VERSION || '0.1.0';
11+
const version = process.env.APP_VERSION || "0.1.0";
912

1013
export async function registerContextRoutes(server: FastifyInstance) {
14+
await server.register(cors, { origin: true });
15+
16+
// Logging middleware
17+
server.addHook("onRequest", async (request) => {
18+
console.log(`[${new Date().toISOString()}] ${request.method} ${request.url}`);
19+
});
20+
server.addHook("onError", async (request, reply, error) => {
21+
console.error(`[${new Date().toISOString()}] ERROR: ${error.message}`);
22+
});
23+
1124
// MCP Discovery Endpoint
12-
server.get('/.well-known/model-context', async () => {
25+
server.get("/.well-known/model-context", async () => {
1326
return {
14-
"@context": "/.well-known/v1.json",
15-
"name": name,
16-
"description": description,
17-
"version": version,
18-
"tags": tags,
19-
"contact": {
20-
"email": email,
21-
"website": website
27+
"@context": "/.well-known/v1.json", // Local context reference
28+
name: name,
29+
description: description,
30+
version: version,
31+
tags, // ES6 shorthand property name
32+
contact: {
33+
email: email,
34+
website: website,
2235
},
23-
"content_endpoint": "/v1/content"
36+
content_endpoint: "/v1/content",
2437
};
2538
});
2639

2740
// Available Models Content
28-
server.get('/v1/content', async () => {
41+
server.get("/v1/content", async () => {
2942
return [
3043
{
3144
title: "Customer Churn Prediction",
32-
content: "Predictive model using logistic regression and XGBoost to identify potential customer churn.",
33-
tags: ["ML", "churn", "classification", "xgboost"]
45+
content:
46+
"Predictive model using logistic regression and XGBoost to identify potential customer churn.",
47+
tags: ["ML", "churn", "classification", "xgboost"],
3448
},
3549
{
3650
title: "Exploratory Data Analysis Report",
37-
content: "Notebook-driven visualisation and summary statistics to uncover key trends and anomalies.",
38-
tags: ["EDA", "data analysis", "notebooks"]
51+
content:
52+
"Notebook-driven visualisation and summary statistics to uncover key trends and anomalies.",
53+
tags: ["EDA", "data analysis", "notebooks"],
3954
},
4055
{
4156
title: "Statistical Hypothesis Testing",
42-
content: "Support for t-tests, chi-square, ANOVA, and p-value interpretation for decision making.",
43-
tags: ["statistics", "hypothesis testing", "inference"]
57+
content:
58+
"Support for t-tests, chi-square, ANOVA, and p-value interpretation for decision making.",
59+
tags: ["statistics", "hypothesis testing", "inference"],
4460
},
4561
{
4662
title: "Time Series Forecasting",
47-
content: "Models using ARIMA and Prophet to forecast trends in business KPIs.",
48-
tags: ["forecasting", "time series", "prophet"]
49-
}
63+
content:
64+
"Models using ARIMA and Prophet to forecast trends in business KPIs.",
65+
tags: ["forecasting", "time series", "prophet"],
66+
},
5067
];
5168
});
69+
70+
// Model Metadata Endpoint
71+
server.get("/v1/model/:modelId", async (request) => {
72+
const { modelId } = request.params as { modelId: string };
73+
if (modelId === "churn") {
74+
return {
75+
id: "churn",
76+
title: "Customer Churn Prediction",
77+
input: { type: "json", example: { customerId: "123", features: [/* ... */] } },
78+
output: { type: "json", example: { churnProbability: 0.87 } },
79+
usage: "POST /v1/predict/churn",
80+
};
81+
}
82+
if (modelId === "eda") {
83+
return {
84+
id: "eda",
85+
title: "Exploratory Data Analysis Report",
86+
input: { type: "csv", example: "data.csv" },
87+
output: { type: "json", example: { summary: "..." } },
88+
usage: "GET /v1/content/eda",
89+
};
90+
}
91+
// ...other models...
92+
return { error: "Model not found" };
93+
});
94+
95+
// Strava Activities Integration Endpoint
96+
server.post("/v1/strava/activities", async (request, reply) => {
97+
const { accessToken, endpoint = "https://www.strava.com/api/v3/athlete/activities", page = 1, per_page = 30 } = request.body as any;
98+
try {
99+
const response = await axios.get(endpoint, {
100+
headers: { Authorization: `Bearer ${accessToken}` },
101+
params: { page, per_page }
102+
});
103+
return response.data;
104+
} catch (error: any) {
105+
console.error(`[${new Date().toISOString()}] Strava API ERROR:`, error.message);
106+
reply.code(500).send({ error: error.message });
107+
}
108+
});
52109
}

0 commit comments

Comments
 (0)