-
Notifications
You must be signed in to change notification settings - Fork 52
Add additional MCP examples #430
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
cbb226e
fix: mcp servers for tools, agent, mods
rushilpatel0 9dca1c8
add readme for mcp servers
rushilpatel0 7548178
fix: add docs for creating an mcp
rushilpatel0 d631c61
Merge branch 'develop' into rpatel/other-mcps
rushilpatel0 684c5f6
Automated pre-commit update
rushilpatel0 7b81660
Merge branch 'develop' into rpatel/other-mcps
rushilpatel0 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
--- | ||
title: "Building a Model Context Protocol server with Codegen" | ||
sidebarTitle: "MCP Server" | ||
icon: "boxes-stacked" | ||
iconType: "solid" | ||
--- | ||
|
||
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 | ||
|
||
<Info>View the full code in our [examples repository](https://github.com/codegen-sh/codegen-sdk/tree/develop/src/codegen/extensions/mcp)</Info> | ||
|
||
|
||
## Setup: | ||
Install the MCP python library | ||
``` | ||
uv pip install mcp | ||
``` | ||
|
||
## Step 1: Setting Up Your MCP Server | ||
|
||
First, let's create a basic MCP server using Codegen's MCP tools: | ||
|
||
server.py | ||
```python | ||
from codegen import Codebase | ||
from mcp.server.fastmcp import FastMCP | ||
from typing import Annotated | ||
# Initialize the codebase | ||
codebase = Codebase.from_repo(".") | ||
|
||
# create the MCP server using FastMCP | ||
mcp = 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") | ||
|
||
``` | ||
|
||
## Step 2: Create the search tool | ||
|
||
Let's implement the semantic search tool. | ||
|
||
server.py | ||
```python | ||
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", programming_language="provide codebase Language") | ||
# use the semantic search tool from codegen.extenstions.tools OR write your own | ||
results = semantic_search(codebase=codebase, query=query) | ||
return results | ||
|
||
.... | ||
``` | ||
|
||
## Run Your MCP Server | ||
|
||
You can run and inspect your MCP server with: | ||
|
||
``` | ||
mcp dev server.py | ||
``` | ||
|
||
If you'd like to integrate this into an IDE checkout out this [setup guide](/introduction/ide-usage#mcp-server-setup) | ||
|
||
And that's a wrap, chime in at our [community | ||
Slack](https://community.codegen.com) if you have quesions or ideas for additional MCP tools/capabilities |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
"""Demo implementation of an agent with Codegen tools.""" | ||
|
||
from langchain_core.messages import BaseMessage | ||
from langchain_core.runnables.history import RunnableWithMessageHistory | ||
|
||
from codegen.extensions.langchain.agent import create_codebase_agent | ||
from codegen.sdk.core.codebase import Codebase | ||
|
||
AGENT_INSTRUCTIONS = """ | ||
Instruction Set for Codegen SDK Expert Agent | ||
|
||
Overview: | ||
This instruction set is designed for an agent that is an expert on the Codegen SDK, specifically the Python library. The agent will be asked questions about the SDK, including classes, utilities, properties, and how to accomplish tasks using the SDK. The goal is to provide helpful responses that assist users in achieving their tasks with the SDK. | ||
|
||
Key Responsibilities: | ||
1. Expertise in Codegen SDK: | ||
- The agent is an expert on the Codegen SDK, with a deep understanding of its components and functionalities. | ||
- It should be able to provide detailed explanations of classes, utilities, and properties defined in the SDK. | ||
|
||
2. Answering Questions: | ||
- The agent will be asked questions about the Codegen SDK, such as: | ||
- "Find all imports" | ||
- "How do I add an import for a symbol?" | ||
- "What is a statement object?" | ||
- Responses should be clear, concise, and directly address the user's query. | ||
|
||
3. Task-Oriented Responses: | ||
- The user is typically accomplishing a task using the Codegen SDK. | ||
- Responses should be helpful toward that goal, providing guidance and solutions that facilitate task completion. | ||
|
||
4. Python Library Focus: | ||
- Assume that questions are related to the Codegen SDK Python library. | ||
- Provide Python-specific examples and explanations when applicable. | ||
|
||
Use the provided agent tools to look up additional information if needed. | ||
By following this instruction set, the agent will be well-equipped to assist users in effectively utilizing the Codegen SDK for their projects. | ||
""" | ||
|
||
|
||
def create_sdk_expert_agent( | ||
codebase: Codebase, | ||
model_name: str = "gpt-4o", | ||
temperature: float = 0, | ||
verbose: bool = True, | ||
) -> RunnableWithMessageHistory: | ||
"""Create an agent with all codebase tools. | ||
|
||
Args: | ||
codebase: The codebase to operate on | ||
model_name: Name of the model to use (default: gpt-4) | ||
temperature: Model temperature (default: 0) | ||
verbose: Whether to print agent's thought process (default: True) | ||
|
||
Returns: | ||
Initialized agent with message history | ||
""" | ||
# Initialize language model | ||
|
||
system_message: BaseMessage = BaseMessage(content=AGENT_INSTRUCTIONS, type="SYSTEM") | ||
|
||
agent = create_codebase_agent(chat_history=[system_message], codebase=codebase, model_name=model_name, temperature=temperature, verbose=verbose) | ||
|
||
return agent |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove this file because it causes import loops if you're import any of the tools elsewhere