-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathbasic.ts
More file actions
179 lines (163 loc) · 4.83 KB
/
Copy pathbasic.ts
File metadata and controls
179 lines (163 loc) · 4.83 KB
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
import { createServer } from 'node:http';
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { createGatewayRuntime } from '@graphql-hive/gateway-runtime';
import { useMCP } from '@graphql-hive/plugin-mcp';
import { createSchema, createYoga } from 'graphql-yoga';
const weatherData: Record<
string,
{ temperature: number; conditions: string; humidity: number }
> = {
'new york': { temperature: 72, conditions: 'Partly Cloudy', humidity: 65 },
london: { temperature: 58, conditions: 'Rainy', humidity: 85 },
tokyo: { temperature: 68, conditions: 'Sunny', humidity: 55 },
sydney: { temperature: 82, conditions: 'Clear', humidity: 45 },
paris: { temperature: 63, conditions: 'Overcast', humidity: 70 },
};
const schema = createSchema({
typeDefs: /* GraphQL */ `
type Query {
"Get current weather data for a location"
weather("City name or postal code" location: String!): Weather!
"Get weather forecast for upcoming days"
forecast(
"City name or postal code"
location: String!
"Number of days to forecast (default 5)"
days: Int = 5
): [ForecastDay!]!
}
type Weather {
"Temperature in Fahrenheit"
temperature: Float!
"Current weather conditions"
conditions: String!
"Humidity percentage"
humidity: Int!
"Location name"
location: String!
}
type ForecastDay {
"Date in YYYY-MM-DD format"
date: String!
"High temperature in Fahrenheit"
high: Float!
"Low temperature in Fahrenheit"
low: Float!
"Expected weather conditions"
conditions: String!
}
`,
resolvers: {
Query: {
weather: (_, { location }: { location: string }) => {
const loc = location.toLowerCase();
const data = weatherData[loc] || {
temperature: 70,
conditions: 'Unknown',
humidity: 50,
};
return { ...data, location };
},
forecast: (_, { days = 5 }: { location: string; days?: number }) => {
const conditions = [
'Sunny',
'Partly Cloudy',
'Cloudy',
'Rainy',
'Clear',
];
const result = [];
const today = new Date();
for (let i = 0; i < days; i++) {
const date = new Date(today);
date.setDate(date.getDate() + i);
result.push({
date: date.toISOString().split('T')[0],
high: Math.round(65 + Math.random() * 20),
low: Math.round(45 + Math.random() * 15),
conditions:
conditions[Math.floor(Math.random() * conditions.length)],
});
}
return result;
},
},
},
});
const subgraphYoga = createYoga({ schema });
const subgraphServer = createServer(subgraphYoga);
subgraphServer.listen(4001, () => {
console.log('Subgraph running at http://localhost:4001/graphql');
});
const mcpOptions = {
name: 'weather-api',
version: '1.0.0',
path: '/mcp',
// .graphql files containing named operations and @mcpTool directives
operationsPath: join(
dirname(fileURLToPath(import.meta.url)),
'operations/weather.graphql',
),
tools: [
// File-based source: references a named operation from operationsPath
{
name: 'get_weather',
source: {
type: 'graphql',
operationName: 'GetWeather',
operationType: 'query',
},
tool: {
title: 'Current Weather',
description: 'Get the current weather for a city',
},
input: {
// Field-level input schema overrides (merged with auto-derived schema)
schema: {
properties: {
location: {
description: 'City name, e.g. "New York", "London", "Tokyo"',
},
},
},
},
},
// File-based source: no overrides, description auto-derived from GraphQL schema
{
name: 'get_forecast',
source: {
type: 'graphql',
operationName: 'GetForecast',
operationType: 'query',
},
},
// Inline source: query defined directly in config (no operations file needed)
{
name: 'get_conditions',
source: {
type: 'inline',
query: `query GetConditions($location: String!) {
weather(location: $location) {
conditions
}
}`,
},
tool: {
title: 'Weather Conditions',
description: 'Get just the weather conditions for a city',
},
},
],
};
const gateway = createGatewayRuntime({
proxy: {
endpoint: 'http://localhost:4001/graphql',
},
plugins: (ctx) => [useMCP(ctx, mcpOptions)],
});
const gatewayServer = createServer(gateway);
gatewayServer.listen(4000, () => {
console.log('Gateway running at http://localhost:4000/graphql');
console.log('MCP endpoint at http://localhost:4000/mcp');
});