V
Visual MCP Workspace
Model Context Protocol Developer Suite
v1.0.0
Loading Highlighter
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import {
CallToolRequestSchema,
ListToolsRequestSchema,
ListResourcesRequestSchema,
ReadResourceRequestSchema,
ListPromptsRequestSchema,
GetPromptRequestSchema
} from "@modelcontextprotocol/sdk/types.js";
/**
* CRITICAL PIPELINE PROTECTION:
* Overwrite console.log, console.info, and console.dir to redirect all stdout logs to stderr.
* Standard stdout MUST be reserved strictly for JSON-RPC 2.0 frames.
* Failing to wrap stdout will corrupt JSON-RPC pipes and crash clients.
*/
const originalLog = console.log;
console.log = console.info = console.dir = (...args) => {
console.error("[INFO Log Redirect]:", ...args);
};
// Initialize MCP Server
const server = new Server(
{
name: "custom-mcp-server",
version: "1.0.0",
},
{
capabilities: {
tools: {},
resources: {},
prompts: {}
},
}
);
// Register tool definitions
server.setRequestHandler(ListToolsRequestSchema, async () => {
return {
tools: [
{
name: "get_weather",
description: "Fetch current weather reports for a given city.",
inputSchema: {
type: "object",
properties: {
"city": {
type: "string",
description: "The city to look up"
}
},
required: ["city"]
}
}
]
};
});
// Handle tool execution requests
server.setRequestHandler(CallToolRequestSchema, async (request) => {
const { name, arguments: args } = request.params;
try {
switch (name) {
case "get_weather": {
if (!args || args["city"] === undefined || typeof args["city"] !== "string") {
throw new Error("Missing or invalid required parameter: city");
}
// TODO: Implement actual tool functionality here
return {
content: [
{
type: "text",
text: `Tool get_weather executed successfully with parameters: ${JSON.stringify(args || {})}`
}
]
};
}
default:
throw new Error(`Unknown tool: ${name}`);
}
} catch (error: any) {
console.error("Error executing tool:", error);
return {
isError: true,
content: [
{
type: "text",
text: `Error: ${error.message || error}`
}
]
};
}
});
// List available resources
server.setRequestHandler(ListResourcesRequestSchema, async () => {
return {
resources: [
{
uri: "logs://system/runtime",
name: "system_logs",
description: "Retrieve current internal system execution logs.",
mimeType: "text/plain"
}
]
};
});
// Read specific resource contents
server.setRequestHandler(ReadResourceRequestSchema, async (request) => {
const { uri } = request.params;
try {
if (uri === "logs://system/runtime") {
return {
contents: [
{
uri: uri,
mimeType: "text/plain",
text: "Sample resource contents for: system_logs"
}
]
};
}
throw new Error(`Resource not found: ${uri}`);
} catch (error: any) {
throw new Error(`Failed to read resource: ${error.message}`);
}
});
// List available prompts
server.setRequestHandler(ListPromptsRequestSchema, async () => {
return {
prompts: [
{
name: "code_review",
description: "Generate a code review prompt with customized guidelines.",
arguments: [
{
name: "code",
description: "Variable code",
required: true
},
{
name: "guidelines",
description: "Variable guidelines",
required: true
}
]
}
]
};
});
// Get specific prompt configuration
server.setRequestHandler(GetPromptRequestSchema, async (request) => {
const { name, arguments: args } = request.params;
try {
switch (name) {
case "code_review": {
if (!args || args["code"] === undefined) {
throw new Error("Missing required argument: code");
}
if (!args || args["guidelines"] === undefined) {
throw new Error("Missing required argument: guidelines");
}
return {
messages: [
{
role: "user",
content: {
type: "text",
text: `You are a code reviewer. Please review the following code:
${args?.["code"] || ''}
Pay attention to: ${args?.["guidelines"] || ''}`
}
}
]
};
}
default:
throw new Error(`Prompt not found: ${name}`);
}
} catch (error: any) {
throw new Error(`Failed to generate prompt: ${error.message}`);
}
});
// Connect and run transport
async function run() {
const transport = new StdioServerTransport();
await server.connect(transport);
console.error("Visual MCP Server running on Stdio transport");
// Prevent zombie processes on host termination
process.stdin.on("close", () => {
console.error("Stdin closed, shutting down server...");
process.exit(0);
});
}
run().catch((error) => {
console.error("Error running MCP server:", error);
process.exit(1);
});