Skip to content

Telemetry #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,22 @@ Add the following to your `mcp.json` for the editor or LLM of choice.
}
```

## Opting Out Of Telemetry

```json
{
"mcpServers": {
"devvit-mcp": {
"command": "npx",
"args": ["-y", "@devvit/mcp"],
"env": {
"DEVVIT_DISABLE_METRICS": "true"
}
}
}
}
```

## Developing on the MCP Server

```sh
Expand Down
10 changes: 9 additions & 1 deletion src/server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
import packageJSON from '../package.json' assert { type: 'json' };
import packageJSON from '../package.json' with { type: 'json' };
import { CallToolRequestSchema, ListToolsRequestSchema } from '@modelcontextprotocol/sdk/types.js';
import { Tool } from './tools/types';
import z from 'zod';
Expand All @@ -8,6 +8,7 @@ import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js'
import { Context } from './utils/context';
import { searchTool } from './tools/search';
import { logsTool } from './tools/logs';
import { sendEvent } from './utils/telemetry';

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const tools: Tool<any>[] = [searchTool, logsTool];
Expand Down Expand Up @@ -44,6 +45,13 @@ export const createServer = (): Server => {
}

try {
// Fire and forget, don't slow down the tool call for telemetry
void sendEvent({
mcp_name: request.params.name,
mcp_args: request.params.arguments,
mcp_step: request.params.arguments?.step as number | undefined,
mcp_args_query: request.params.arguments?.query as string | undefined,
});
const result = await tool.handler({ params: request.params.arguments, context });
return result;
} catch (error) {
Expand Down
168 changes: 168 additions & 0 deletions src/utils/telemetry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
/** Lifted from: https://github.snooguts.net/reddit/reddit-devplatform-monorepo/tree/main/packages/cli/src */
import os from 'os';
import { version } from '../../package.json';
import path from 'path';
import fs from 'fs/promises';
import crypto from 'crypto';
import { logger } from './logger';

async function isFile(path: string): Promise<boolean> {
try {
const stat = await fs.stat(path);
return stat.isFile();
} catch {
return false;
}
}

/** @type {boolean} See envvar.md. */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since this is TypeScript, we can replace JSDoc type annotations with TypeScript types (but the rest of the doc is nice!).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I sorta wanna keep the copy pasta from the CLI in this file so it's more of a port and not a rewrite. Although I do agree with most of your feedback

const MY_PORTAL_ENABLED = !!process.env.MY_PORTAL && process.env.MY_PORTAL !== '0';

const STAGE_USER_NAME =
// Not every username is `first-last`, if `MY_PORTAL` looks like a username use that directly
MY_PORTAL_ENABLED && process.env.MY_PORTAL?.includes('-')
? process.env.MY_PORTAL.toLowerCase()
: os.userInfo().username.replace(/\./g, '-');

const DEVVIT_PORTAL_URL = (() => {
if (MY_PORTAL_ENABLED) {
return `https://reddit-service-devvit-dev-portal.${STAGE_USER_NAME}.snoo.dev` as const;
}

return 'https://developers.reddit.com' as const;
})();

const DEVVIT_PORTAL_API = `${DEVVIT_PORTAL_URL}/api` as const;

type HeaderTuple = readonly [key: string, val: string];

const HEADER_USER_AGENT = (): HeaderTuple => [
'user-agent',
`Devvit/MCP/${version} Node/${process.version.replace(/^v/, '')}`,
];

const HEADER_DEVVIT_MCP = (): HeaderTuple => ['x-devvit-mcp', 'true'];

const HEADER_DEVVIT_CANARY = (val: string): HeaderTuple => {
return ['devvit-canary', val];
};

const DIR_SUFFIX = MY_PORTAL_ENABLED ? `-${STAGE_USER_NAME}` : '';

/** @type {string} Relative Devvit CLI configuration directory filename. */
const DEVVIT_DIR_NAME = `${process.env.DEVVIT_DIR_NAME || '.devvit'}${DIR_SUFFIX}`;

/**
* @type {string} Absolute filename of the Devvit CLI configuration directory.
*/
const DOT_DEVVIT_DIR_FILENAME = process.env.DEVVIT_ROOT_DIR
? path.join(process.env.DEVVIT_ROOT_DIR, DEVVIT_DIR_NAME)
: path.join(os.homedir(), DEVVIT_DIR_NAME);

function getHeaders(): Headers {
const headers = new Headers();
headers.set(...HEADER_USER_AGENT());
headers.set(...HEADER_DEVVIT_MCP());

if (process.env.DEVVIT_CANARY) {
logger.warn(`Warning: setting devvit-canary to "${process.env.DEVVIT_CANARY}"`);
headers.set(...HEADER_DEVVIT_CANARY(process.env.DEVVIT_CANARY));
}

return headers;
}

function getTelemetrySessionIdFilename(): string {
return path.join(DOT_DEVVIT_DIR_FILENAME, 'session-id');
}

function getTokenFilename(): string {
return path.join(DOT_DEVVIT_DIR_FILENAME, 'token');
}

function getMetricsOptOutFile(): string {
return path.join(DOT_DEVVIT_DIR_FILENAME, 'opt-out-metrics');
}

async function isMetricsEnabled(): Promise<boolean> {
if (process.env.DEVVIT_DISABLE_METRICS) {
return false;
}

const optOutFile = getMetricsOptOutFile();
return !(await isFile(optOutFile));
}

async function getTelemetrySessionId(): Promise<string> {
const sessionIdFilename = getTelemetrySessionIdFilename();
const isSessionIdFileCreated = await isFile(sessionIdFilename);

if (isSessionIdFileCreated) {
return await fs.readFile(sessionIdFilename, 'utf-8');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we catch here and when writing?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ported code so I didn't dig in too closely

}

const sessionId = crypto.randomUUID();
await fs.mkdir(DOT_DEVVIT_DIR_FILENAME, { recursive: true });
await fs.writeFile(sessionIdFilename, sessionId, 'utf-8');
return sessionId;
}

async function getToken(): Promise<string | undefined> {
const tokenFilename = getTokenFilename();
const isTokenFileCreated = await isFile(tokenFilename);

if (!isTokenFileCreated) return;

try {
const contents = await fs.readFile(tokenFilename, 'utf-8');
return JSON.parse(contents).token;
} catch (error) {
return undefined;
}
}

export const sendEvent = async (
args: {
mcp_name: string;
mcp_args?: Record<string, unknown> | undefined;
mcp_step?: number | undefined;
/** The query from devvit search */
mcp_args_query?: string | undefined;
},
force: boolean = false
) => {
const shouldTrack = force || (await isMetricsEnabled());
if (!shouldTrack) return;

const sessionId = await getTelemetrySessionId();
const eventWithSession = {
structValue: {
source: 'devplatform_mcp',
action: 'call',
noun: 'tool',
devplatform: args,
session: {
id: sessionId,
},
},
};

const headers = getHeaders();

headers.set('content-type', 'application/json');

const token = await getToken();
if (token) {
headers.set('authorization', `bearer ${token}`);
}

try {
await fetch(`${DEVVIT_PORTAL_API}/events/devvit.dev_portal.Events/SendEvent`, {
method: 'POST',
headers,
body: JSON.stringify(eventWithSession),
});
} catch (error) {
// We don't care if it fails!
}
};