Skip to content

feat(mcp): prompts #7070

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

Draft
wants to merge 4 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
2 changes: 1 addition & 1 deletion js/packages/phoenix-mcp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"license": "Apache-2.0",
"dependencies": {
"@arizeai/phoenix-client": "workspace:*",
"@modelcontextprotocol/sdk": "^1.8.0",
"@modelcontextprotocol/sdk": "^1.9.0",
"glob": "^11.0.1",
"minimist": "^1.2.8",
"zod": "^3.24.2"
Expand Down
22 changes: 14 additions & 8 deletions js/packages/phoenix-mcp/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { initializeDatasetTools } from "./datasetTools.js";
import { initializeExperimentTools } from "./experimentTools.js";
import { initializePromptTools } from "./promptTools.js";
import { initializeReadmeResources } from "./readmeResource.js";

import { initializePrompts } from "./prompts.js";
const argv = minimist(process.argv.slice(2));

// Initialize Phoenix client
Expand All @@ -23,15 +23,21 @@ const client = createClient({
});

// Create server instance
const server = new McpServer({
name: "phoenix-mcp-server",
version: "1.0.0",
capabilities: {
resources: {},
tools: {},
const server = new McpServer(
{
name: "phoenix-mcp-server",
version: "1.0.0",
},
});
{
capabilities: {
resources: {},
tools: {},
prompts: {},
},
}
);

initializePrompts({ client, server });
initializePromptTools({ client, server });
initializeExperimentTools({ client, server });
initializeDatasetTools({ client, server });
Expand Down
140 changes: 140 additions & 0 deletions js/packages/phoenix-mcp/src/prompts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
import { PhoenixClient } from "@arizeai/phoenix-client";
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import {
GetPromptRequestSchema,
ListPromptsRequestSchema,
} from "@modelcontextprotocol/sdk/types.js";

const parseVariablesMustache = (template: string) => {
const regex = /\{\{([^{}]+)\}\}/g;
const variables = [];
let match;
while ((match = regex.exec(template))) {
variables.push(match[1]);
}
return variables;
};

const parseArgumentsFString = (template: string) => {
const regex = /\{([^{}]+)\}/g;
const variables = [];
let match;
while ((match = regex.exec(template))) {
variables.push(match[1]);
}
return variables;
};

export const initializePrompts = async ({
client,
server,
}: {
client: PhoenixClient;
server: McpServer;
}) => {
server.server.setRequestHandler(ListPromptsRequestSchema, async () => {
const promptsResponse = await client.GET("/v1/prompts");

if (!promptsResponse.data) {
return {
prompts: [],
};
}

// Get all the prompts and parse out the arguments
const prompts = await Promise.all(
promptsResponse.data.data.map(async (prompt) => {
const promptVersionResponse = await client.GET(
"/v1/prompts/{prompt_identifier}/latest",
{
params: {
path: {
prompt_identifier: prompt.name,
},
},
}
);

const args: string[] = [];
const template = promptVersionResponse.data?.data.template;
const format = promptVersionResponse.data?.data.template_format;
const parser =
format === "F_STRING"
? parseArgumentsFString
: parseVariablesMustache;
if (template && template.type === "chat") {
template.messages.forEach((message) => {
const content = message.content;
if (typeof content === "string") {
args.push(...parser(content));
}
});
}
return {
name: prompt.name,
description: prompt.description,
arguments: args,
};
})
);
return { prompts };
});

server.server.setRequestHandler(GetPromptRequestSchema, async (request) => {
// const args = request.params.arguments || {};
// Get the latest version of the prompt
const promptVersionResponse = await client.GET(
"/v1/prompts/{prompt_identifier}/latest",
{
params: {
path: {
prompt_identifier: request.params.name,
},
},
}
);

const template = promptVersionResponse.data?.data.template;
let messages: { role: string; content: { type: string; text: string } }[] =
[];
if (template && template.type === "chat") {
messages = template.messages
.map((message) => {
if (message.role === "system") {
return undefined;
}
const messageContent = message.content;
if (typeof messageContent === "string") {
return {
role: message.role,
content: {
type: "text",
text: messageContent,
},
};
}
if (Array.isArray(messageContent)) {
if (
messageContent.length === 1 &&
messageContent[0].type === "text"
) {
const messageContentText = messageContent[0].text;
return {
role: message.role,
content: {
type: "text",
text: messageContentText,
},
};
}
}
return undefined;
})
.filter((message) => message !== undefined);
}
return {
description: promptVersionResponse.data?.data.description,
messages: messages,
};
});
};
18 changes: 9 additions & 9 deletions js/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading