Skip to content

Commit 7e342ed

Browse files
authored
feat: Add configurable source variable to inform service requests. (#54)
* feat: Add configurable source env variable. * feat: Updating README for env usage. * test: ensure API calls for instructions and RAG include the new env variable.
1 parent b883957 commit 7e342ed

4 files changed

Lines changed: 304 additions & 268 deletions

File tree

packages/code-assist/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,24 @@ Or with an environment variable:
269269

270270
If the specified port is unavailable, the server will automatically find and start on a random available port.
271271

272+
### Source Configuration
273+
274+
You can specify a `SOURCE` environment variable to track the origin of the requests. The default value is `github`.
275+
276+
**Example: Set source to `my_company_name`**
277+
278+
In your `mcp.json`:
279+
280+
```json
281+
"google-maps-platform-code-assist": {
282+
"command": "npx",
283+
"args": ["-y", "@googlemaps/code-assist-mcp@latest"],
284+
"env": {
285+
"SOURCE": "my_company_name"
286+
}
287+
}
288+
```
289+
272290
<!-- [END maps_Settings] -->
273291

274292
-----

packages/code-assist/config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,6 @@
1616

1717
export const ragEndpoint = "https://rag-230009110455.us-central1.run.app"
1818

19+
export const SOURCE = process.env.SOURCE || 'github';
20+
1921
export const DEFAULT_CONTEXTS = ["Google Maps Platform"];

packages/code-assist/index.ts

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,33 +22,33 @@ import { Server } from '@modelcontextprotocol/sdk/server/index.js';
2222
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
2323
import { StreamableHTTPServerTransport } from '@modelcontextprotocol/sdk/server/streamableHttp.js';
2424
import { Tool, CallToolRequest, CallToolRequestSchema, ListToolsRequestSchema, Resource, ListResourcesRequestSchema, ReadResourceRequest, ReadResourceRequestSchema, isInitializeRequest } from '@modelcontextprotocol/sdk/types.js';
25-
import { ragEndpoint, DEFAULT_CONTEXTS } from './config.js';
25+
import { ragEndpoint, DEFAULT_CONTEXTS, SOURCE } from './config.js';
2626
import axios from 'axios';
2727

2828
// MCP Streamable HTTP compliance: Accept header validation
2929
function validateAcceptHeader(req: Request): boolean {
30-
const acceptHeader = req.headers.accept;
31-
if (!acceptHeader) return false;
32-
33-
const acceptedTypes = acceptHeader.split(',').map(type => type.trim().split(';')[0]);
34-
return acceptedTypes.includes('application/json') && acceptedTypes.includes('text/event-stream');
30+
const acceptHeader = req.headers.accept;
31+
if (!acceptHeader) return false;
32+
33+
const acceptedTypes = acceptHeader.split(',').map(type => type.trim().split(';')[0]);
34+
return acceptedTypes.includes('application/json') && acceptedTypes.includes('text/event-stream');
3535
}
3636

3737
// Feature 4: Origin header validation for DNS rebinding protection
3838
function validateOriginHeader(req: Request): boolean {
39-
const origin = req.headers.origin;
40-
41-
// Allow requests without Origin header (server-to-server)
42-
if (!origin) return true;
43-
44-
// For development, allow localhost origins
45-
if (process.env.NODE_ENV !== 'production') {
46-
return origin.startsWith('http://localhost') || origin.startsWith('https://localhost');
47-
}
48-
49-
// In production, validate against allowed origins
50-
const allowedOrigins = process.env.ALLOWED_ORIGINS?.split(',') || [];
51-
return allowedOrigins.includes(origin);
39+
const origin = req.headers.origin;
40+
41+
// Allow requests without Origin header (server-to-server)
42+
if (!origin) return true;
43+
44+
// For development, allow localhost origins
45+
if (process.env.NODE_ENV !== 'production') {
46+
return origin.startsWith('http://localhost') || origin.startsWith('https://localhost');
47+
}
48+
49+
// In production, validate against allowed origins
50+
const allowedOrigins = process.env.ALLOWED_ORIGINS?.split(',') || [];
51+
return allowedOrigins.includes(origin);
5252
}
5353

5454
const RetrieveGoogleMapsPlatformDocs: Tool = {
@@ -109,7 +109,11 @@ export async function getUsageInstructions(server: Server) {
109109
return usageInstructions;
110110
}
111111
try {
112-
const ragResponse = await axios.get(ragEndpoint.concat("/instructions"));
112+
const ragResponse = await axios.get(ragEndpoint.concat("/instructions"), {
113+
params: {
114+
source: SOURCE
115+
}
116+
});
113117

114118
usageInstructions = [
115119
ragResponse.data.systemInstructions,
@@ -223,7 +227,8 @@ export async function handleCallTool(request: CallToolRequest, server: Server) {
223227
// Call the RAG service:
224228
const ragResponse = await axios.post(ragEndpoint.concat("/chat"), {
225229
message: prompt,
226-
contexts: contexts
230+
contexts: contexts,
231+
source: SOURCE
227232
});
228233

229234
let mcpResponse = {

0 commit comments

Comments
 (0)