Building a Model Context Protocol server with Codegen
Learn how to build a Model Context Protocol (MCP) server that enables AI models to understand and manipulate code using Codegen’s powerful tools.This guide will walk you through creating an MCP server that can provide semantic code search
First, let’s create a basic MCP server using Codegen’s MCP tools:server.py
Copy
Ask AI
from codegen import Codebasefrom mcp.server.fastmcp import FastMCPfrom typing import Annotated# Initialize the codebasecodebase = Codebase.from_repo(".")# create the MCP server using FastMCPmcp = FastMCP(name="demo-mcp", instructions="Use this server for semantic search of codebases")if __name__ == "__main__": # Initialize and run the server print("Starting demo mpc server...") mcp.run(transport="stdio")
Let’s implement the semantic search tool.server.py
Copy
Ask AI
from codegen.extensions.tools.semantic_search import semantic_search....@mcp.tool('codebase_semantic_search', "search codebase with the provided query")def search(query: Annotated[str, "search query to run against codebase"]): codebase = Codebase("provide location to codebase", language="provide codebase Language") # use the semantic search tool from codegen.extensions.tools OR write your own results = semantic_search(codebase=codebase, query=query) return results....
If you’d like to integrate this into an IDE checkout out this setup guideAnd that’s a wrap, chime in at our community
Slack if you have questions or ideas for additional MCP tools/capabilities