|
| 1 | +""" |
| 2 | +Copyright (c) 2025 DevRev, Inc. |
| 3 | +SPDX-License-Identifier: MIT |
| 4 | +
|
| 5 | +This module implements the MCP server for DevRev integration. |
| 6 | +""" |
| 7 | + |
| 8 | +import asyncio |
| 9 | +import os |
| 10 | +import requests |
| 11 | + |
| 12 | +from mcp.server.models import InitializationOptions |
| 13 | +import mcp.types as types |
| 14 | +from mcp.server import NotificationOptions, Server |
| 15 | +from pydantic import AnyUrl |
| 16 | +import mcp.server.stdio |
| 17 | +from .utils import make_devrev_request |
| 18 | + |
| 19 | +server = Server("devrev_mcp") |
| 20 | + |
| 21 | +@server.list_tools() |
| 22 | +async def handle_list_tools() -> list[types.Tool]: |
| 23 | + """ |
| 24 | + List available tools. |
| 25 | + Each tool specifies its arguments using JSON Schema validation. |
| 26 | + """ |
| 27 | + return [ |
| 28 | + types.Tool( |
| 29 | + name="search", |
| 30 | + description="Search DevRev using the provided query", |
| 31 | + inputSchema={ |
| 32 | + "type": "object", |
| 33 | + "properties": { |
| 34 | + "query": {"type": "string"}, |
| 35 | + "namespace": {"type": "string", "enum": ["article", "issue", "ticket"]}, |
| 36 | + }, |
| 37 | + "required": ["query", "namespace"], |
| 38 | + }, |
| 39 | + ), |
| 40 | + types.Tool( |
| 41 | + name="get_object", |
| 42 | + description="Get all information about a DevRev issue and ticket using its ID", |
| 43 | + inputSchema={ |
| 44 | + "type": "object", |
| 45 | + "properties": { |
| 46 | + "id": {"type": "string"}, |
| 47 | + }, |
| 48 | + "required": ["id"], |
| 49 | + }, |
| 50 | + ) |
| 51 | + ] |
| 52 | + |
| 53 | +@server.call_tool() |
| 54 | +async def handle_call_tool( |
| 55 | + name: str, arguments: dict | None |
| 56 | +) -> list[types.TextContent | types.ImageContent | types.EmbeddedResource]: |
| 57 | + """ |
| 58 | + Handle tool execution requests. |
| 59 | + Tools can modify server state and notify clients of changes. |
| 60 | + """ |
| 61 | + if name == "search": |
| 62 | + if not arguments: |
| 63 | + raise ValueError("Missing arguments") |
| 64 | + |
| 65 | + query = arguments.get("query") |
| 66 | + if not query: |
| 67 | + raise ValueError("Missing query parameter") |
| 68 | + |
| 69 | + namespace = arguments.get("namespace") |
| 70 | + if not namespace: |
| 71 | + raise ValueError("Missing namespace parameter") |
| 72 | + |
| 73 | + response = make_devrev_request( |
| 74 | + "search.hybrid", |
| 75 | + {"query": query, "namespace": namespace} |
| 76 | + ) |
| 77 | + if response.status_code != 200: |
| 78 | + error_text = response.text |
| 79 | + return [ |
| 80 | + types.TextContent( |
| 81 | + type="text", |
| 82 | + text=f"Search failed with status {response.status_code}: {error_text}" |
| 83 | + ) |
| 84 | + ] |
| 85 | + |
| 86 | + search_results = response.json() |
| 87 | + return [ |
| 88 | + types.TextContent( |
| 89 | + type="text", |
| 90 | + text=f"Search results for '{query}':\n{search_results}" |
| 91 | + ) |
| 92 | + ] |
| 93 | + elif name == "get_object": |
| 94 | + if not arguments: |
| 95 | + raise ValueError("Missing arguments") |
| 96 | + |
| 97 | + id = arguments.get("id") |
| 98 | + if not id: |
| 99 | + raise ValueError("Missing id parameter") |
| 100 | + |
| 101 | + response = make_devrev_request( |
| 102 | + "works.get", |
| 103 | + {"id": id} |
| 104 | + ) |
| 105 | + if response.status_code != 200: |
| 106 | + error_text = response.text |
| 107 | + return [ |
| 108 | + types.TextContent( |
| 109 | + type="text", |
| 110 | + text=f"Get object failed with status {response.status_code}: {error_text}" |
| 111 | + ) |
| 112 | + ] |
| 113 | + |
| 114 | + object_info = response.json() |
| 115 | + return [ |
| 116 | + types.TextContent( |
| 117 | + type="text", |
| 118 | + text=f"Object information for '{id}':\n{object_info}" |
| 119 | + ) |
| 120 | + ] |
| 121 | + else: |
| 122 | + raise ValueError(f"Unknown tool: {name}") |
| 123 | + |
| 124 | +async def main(): |
| 125 | + # Run the server using stdin/stdout streams |
| 126 | + async with mcp.server.stdio.stdio_server() as (read_stream, write_stream): |
| 127 | + await server.run( |
| 128 | + read_stream, |
| 129 | + write_stream, |
| 130 | + InitializationOptions( |
| 131 | + server_name="devrev_mcp", |
| 132 | + server_version="0.1.1", |
| 133 | + capabilities=server.get_capabilities( |
| 134 | + notification_options=NotificationOptions(), |
| 135 | + experimental_capabilities={}, |
| 136 | + ), |
| 137 | + ), |
| 138 | + ) |
0 commit comments