Skip to content

Commit

Permalink
Merge pull request #25 from tinybirdco/add-typescript-readme
Browse files Browse the repository at this point in the history
add typescript readme
  • Loading branch information
alrocar authored Dec 9, 2024
2 parents 5d3afb6 + a89934f commit ec20aa4
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 13 deletions.
69 changes: 56 additions & 13 deletions mcp-server-analytics/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ Click the button below to deploy the project to your Tinybird Workspace.
</a>
</p>

## 2. Send log events using Python
## 2. Send log events

Add the following to your `requirements.txt` file:
### Using Python

Add the following dependency to your `requirements.txt` file:

```
tinybird-python-sdk>=0.1.6
Expand All @@ -39,26 +41,67 @@ handler.setFormatter(formatter)
logger.addHandler(handler)
```

Your `TB_WRITE_TOKEN` can be found in the [Tinybird dashboard](https://app.tinybird.co/tokens) with the name `mcp_public_write_token`.

Your `TB_API_URL` is the URL of your Tinybird region.
To properly process your log events, add an extra dictionary with the `tool`, `resource`, `prompt`, `mcp_server_version` and `session` keys when it applies. That way the provided Tinybird Workspace will be able to process metrics by tool, resource, prompt and session.

![](./region.png)
```python
logger.info(f"handle_call_tool {name}", extra={"session": session, "tool": name, "mcp_server_version": "0.1.4"})
```

Start sending your MCP logs and errors:
See some sample logger calls [here](https://github.com/tinybirdco/mcp-tinybird/blob/main/src/mcp_tinybird/server.py)

### Using TypeScript

```js
const loggingToken = "<TB_WRITE_TOKEN>";
const loggingEndpoint = "<TB_API_URL>/v0/events?name=mcp_logs";
const loggingSession = crypto.randomUUID();

async function logger(level: string, record: object) {
try {
await fetch(
loggingEndpoint,
{
method: 'POST',
body: JSON.stringify({
timestamp: new Date().toISOString(),
session: loggingSession,
level: level,
record: JSON.stringify(record)
}),
headers: { Authorization: `Bearer ${loggingToken}` }
}
)
.then((res: Response) => { /**process.stderr.write("logged");**/ });
} catch (error) {
// process.stderr.write("error logging");
}
```
```python
logger.info(f"handle_call_tool {name}", extra={**extra, "tool": name})
To properly process your log events, add the following keys to the `record` JSON object:
```js
record = {
"appName": "mcp-tinybird",
"funcName": "handle_call_tool",
"tool": "run-select-query",
"prompt": "",
"resource": "",
"level": "info",
"version": "0.1.4",
"message": "this is a message"
}
```
Make sure you include an extra dictionary with the `tool`, `resource`, `prompt` and `session` keys when it applies.
See some sample logger calls [here](See [ClaudeKeep](https://github.com/sdairs/claudekeep/blob/main/apps/mcp/src/index.ts)
### Your Tinybird credentials
See logger calls [here](https://github.com/tinybirdco/mcp-tinybird/blob/main/src/mcp_tinybird/server.py)
Your `TB_WRITE_TOKEN` can be found in the [Tinybird dashboard](https://app.tinybird.co/tokens) with the name `mcp_public_write_token`.
Your `TB_API_URL` is the URL of your Tinybird region.
### Send log events using TypeScript
![](./region.png)
TODO
## 3. Monitor with Grafana and Prometheus
Expand Down
19 changes: 19 additions & 0 deletions mcp-server-analytics/tinybird/pipes/mv_mcp_logs_pipe.pipe
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
NODE mv_mcp_logs_pipe_0
SQL >
SELECT
JSONExtractString(record, 'appName') as app_name,
timestamp as datetime,
JSONExtractString(record, 'funcName') as func_name,
JSONExtractString(record, 'tool') as tool,
JSONExtractString(record, 'prompt') as prompt,
JSONExtractString(record, 'resource') as resource,
level as level,
session as session,
JSONExtractString(record, 'version') as version,
JSONExtractString(record, 'message') as message
FROM mcp_logs

TYPE materialized
DATASOURCE mcp_monitoring


0 comments on commit ec20aa4

Please sign in to comment.