Skip to content
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

WIP: MCP #406

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions packages/mcp-server/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist
36 changes: 36 additions & 0 deletions packages/mcp-server/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Fiberplane Model Context Protocol Server

This is a proof of concept model context protocol server for querying Fiberplane Studio local API using the Model Context Protocol.

## Setting up using Claude Desktop

If you have Claude Desktop installed, update the configuration to use the local MCP server.

```json
{
"mcpServers": {
"fiberplane": {
"command": "npx",
"args": [
"@fiberplane/mcp-server-fiberplane"
]
}
}
}
```

> **Note**
> If you're using NVM or FNM or any other Node version manager, Claude Desktop will not be able to find the `node` command. Use the following snippet as reference. Issue tracked [here](https://github.com/modelcontextprotocol/servers/issues/64)
>
> ```json
> {
> "mcpServers": {
> "fiberplane": {
> "command": "<path_to_node>",
> "args": [
> "<path_to_globally_installed_npm_packages_>/@fiberplane/mcp-server-fiberplane/dist/index.js"
> ]
> }
> }
> }
> ```
189 changes: 189 additions & 0 deletions packages/mcp-server/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import {
type CallToolRequest,
CallToolRequestSchema,
ListToolsRequestSchema,
type Tool,
} from "@modelcontextprotocol/sdk/types.js";

const DEFAULT_FIBERPLANE_STUDIO_URL = "http://localhost:8788";

// Tool definitions
const listTracesTool: Tool = {
name: "fiberplane_list_traces",
description:
"Retrieves a list of all traces, showing the root spans for each trace",
inputSchema: {
type: "object",
properties: {},
},
};

const getSpansForTraceTool: Tool = {
name: "fiberplane_get_spans_for_trace",
description: "Retrieves all spans for a specific trace ID",
inputSchema: {
type: "object",
properties: {
traceId: {
type: "string",
description: "The ID of the trace to fetch spans for",
},
},
required: ["traceId"],
},
};

const listAllRegisteredRoutesTool: Tool = {
name: "fiberplane_list_all_registered_routes",
description: "Lists all registered routes in the studio",
inputSchema: {
type: "object",
properties: {},
},
};

const listAllRequestsTool: Tool = {
name: "fiberplane_list_all_requests",
description: "Lists all requests, logged in the studio",
inputSchema: {
type: "object",
properties: {},
},
};

async function main() {
console.log("Starting Fiberplane MCP Server ...");

const server = new Server(
{ name: "Fiberplane MCP Server", version: "0.1.0" },
{
capabilities: {
tools: {
fiberplane_list_traces: listTracesTool,
fiberplane_get_spans_for_trace: getSpansForTraceTool,
fiberplane_list_all_registered_routes: listAllRegisteredRoutesTool,
fiberplane_list_all_requests: listAllRequestsTool,
},
},
},
);

server.setRequestHandler(
CallToolRequestSchema,
async (request: CallToolRequest) => {
console.log("Received tool call request:", request);
try {
if (!request.params.arguments) {
throw new Error("No arguments provided");
}

const tool = request.params.name;
const baseUrl =
process.env.FIBERPLANE_STUDIO_URL || DEFAULT_FIBERPLANE_STUDIO_URL;

switch (tool) {
case "fiberplane_list_traces": {
const response = await fetch(`${baseUrl}/v1/traces`);
const traces = await response.json();
console.log("traces", traces);
return {
content: [
{
type: "text",
text: JSON.stringify(traces),
},
],
};
}

case "fiberplane_get_spans_for_trace": {
const { traceId } = request.params.arguments;
if (!traceId) {
throw new Error("No traceId provided");
}
const response = await fetch(
`${baseUrl}/v1/traces/${traceId}/spans`,
);
const spans = await response.json();
console.log("spans", spans);
return {
content: [
{
type: "text",
text: JSON.stringify(spans),
},
],
};
}

case "fiberplane_list_all_registered_routes": {
const response = await fetch(`${baseUrl}/v0/app-routes`);
const routes = await response.json();
console.log("routes", routes);
return {
content: [
{
type: "text",
text: JSON.stringify(routes),
},
],
};
}

case "fiberplane_list_all_requests": {
const response = await fetch(`${baseUrl}/v0/all-requests`);
const requests = await response.json();
console.log("requests", requests);
return {
content: [
{
type: "text",
text: JSON.stringify(requests),
},
],
};
}

default: {
throw new Error(`Unknown tool: ${tool}`);
}
}
} catch (error) {
console.error("Error executing tool:", error);
return {
content: [
{
type: "text",
text: JSON.stringify({
error: error instanceof Error ? error.message : String(error),
}),
},
],
};
}
},
);

server.setRequestHandler(ListToolsRequestSchema, async () => {
console.log("Received list tools request");
return {
tools: [
listTracesTool,
getSpansForTraceTool,
listAllRegisteredRoutesTool,
listAllRequestsTool,
],
};
});

const transport = new StdioServerTransport();
console.log("Connecting server to transport ...");
await server.connect(transport);
}

main().catch((error) => {
console.error("Error starting MCP server", error);
process.exit(1);
});
33 changes: 33 additions & 0 deletions packages/mcp-server/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "@fiberplane/mcp-server-fiberplane",
"type": "module",
"version": "0.1.1",
"access": "public",
"description": "MCP server for Fiberplane Studio",
"main": "dist/index.js",
"bin": {
"mcp-server-fiberplane": "./dist/src/index.js"
},
"scripts": {
"build": "tsc",
"dev": "tsc --watch",
"prepublish": "pnpm build"
},
"files": [
"dist",
"README.md"
],
"keywords": [],
"publishConfig": {
"access": "public"
},
"author": "Fiberplane<info@fiberplane.com>",
"license": "MIT",
"dependencies": {
"@modelcontextprotocol/sdk": "^1.0.3"
},
"devDependencies": {
"@types/node": "^22.10.1",
"typescript": "^5.7.2"
}
}
24 changes: 24 additions & 0 deletions packages/mcp-server/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"compilerOptions": {
"target": "esnext",
"lib": [
"esnext"
],
"module": "NodeNext",
"types": [
"node"
],
"esModuleInterop": true,
"outDir": "./dist",
"rootDir": ".",
"strict": true,
"skipLibCheck": true
},
"include": [
"index.ts",
],
"exclude": [
"node_modules",
"dist"
]
}
Loading
Loading