Skip to content
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

Adding cody mcp #6589

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open

Adding cody mcp #6589

wants to merge 14 commits into from

Conversation

arafatkatze
Copy link
Contributor

@arafatkatze arafatkatze commented Jan 10, 2025

Related OpenContext PR

Purposely waiting to do the final steps of the PR AFTER the base merge conflicts from Bee's PR is resolved and a demo is shared and discussed properly.

Test plan

Example MCP server implementation

  1. for Echo Tool
    https://www.loom.com/share/6c605cb1ccd4479ea269743ae6dd3529
  • Pull the MCP server from this specific branch https://github.com/arafatkatze/servers/tree/mcp-server-ara
  • cd src/everything/
  • npm run build
  • You would also have to run pnpm openctx:link to be able to link the local OpenCTX latest repo
  • node dist/test_mcp.js If this works fine for you that means the local MCP is running

Now run the branch in the debugger and set the settings in vscode as

  "openctx.providers": {
    "mcp": {
      "enabled": true, 
      "uri": "file:///path_to_servers/src/everything/dist/index.js",
      "toolNameQuery": "echo"
    }
  },

Please make sure to have three slashes /// in the uri after file.
For this example the toolnamequery is echo.

  1. for Postgres tool

Now run the branch in the debugger and set the settings in vscode as

  "openctx.providers": {
    "mcp": {
      "enabled": true, 
      "uri": "file:///Users/arafatkhan/Desktop/servers/src/postgres/dist/index.js",
      "toolNameQuery": "query",
      "args": ["postgresql://postgres:postgres@localhost:5433/postgres"]
    }
  },

Please make sure to have three slashes /// in the uri after file.
For this example the toolnamequery is query(that's defined in the MCP)

Now in deep cody ask the question:

from the local run of the postgres tool tell me the name of the users from the users table

NOTE: You gotta run the MCP with docker locally and make sure that postgres works properly for you before you do this.

For me I had

➜  postgres git:(mcp-server-ara) ✗ psql -h localhost -p 5433 -U postgres -d postgres
psql (16.6 (Homebrew), server 17.2 (Debian 17.2-1.pgdg120+1))
WARNING: psql major version 16, server major version 17.
         Some psql features might not work.
Type "help" for help.

postgres=# list
postgres-# ;
ERROR:  syntax error at or near "list"
LINE 1: list
        ^
postgres=# CREATE TABLE users (
    id SERIAL PRIMARY KEY,
    username VARCHAR(50) NOT NULL,
    email VARCHAR(255) NOT NULL UNIQUE,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE
postgres=# INSERT INTO users (username, email) VALUES 
    ('john_doe', 'john@example.com'),
    ('jane_smith', 'jane@example.com');
INSERT 0 2
postgres=# \l           -- List all databases
\dt          -- List all tables
\d users     -- Describe the users table structure
\x           -- Toggle expanded display for better readability
SELECT * FROM users;  -- View table contents
ERROR:  column d.daticulocale does not exist
LINE 8:   d.daticulocale as "ICU Locale",
          ^
HINT:  Perhaps you meant to reference the column "d.datlocale".
postgres=# SELECT * FROM users;
 id |  username  |      email       |          created_at           
----+------------+------------------+-------------------------------
  1 | john_doe   | john@example.com | 2025-01-16 23:48:11.031239+00
  2 | jane_smith | jane@example.com | 2025-01-16 23:48:11.031239+00
(2 rows)

postgres=# exit
➜  postgres git:(mcp-server-ara) ✗ 

https://www.loom.com/share/a933fef57b6c42b5b5e06eced9e91c9f

@arafatkatze arafatkatze force-pushed the adding-cody-mcp branch 2 times, most recently from 05f7fc9 to e0bc155 Compare January 11, 2025 17:52
@arafatkatze arafatkatze marked this pull request as ready for review January 14, 2025 16:37
Copy link
Contributor

@abeatrix abeatrix left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just noticed you are on an old branch so probably unaware of the changes we have on main where we automatically build the tool from openCtx providers via setupOpenCtxProviderListener. In that method we can get the mentions from the MCP and convert them using openCtxProviderMetadata so that we can create the tools for all openCtx providers using the same createOpenCtxTools method.

Let me know if this is confusing or if you have any concerns about this approach!

Comment on lines 249 to 316
public parse(): string[] {
try {
JSON.parse(this.unprocessedText)
} catch {
return []
}
const unparsedText = this.unprocessedText
this.reset()
return [unparsedText]
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public parse(): string[] {
try {
JSON.parse(this.unprocessedText)
} catch {
return []
}
const unparsedText = this.unprocessedText
this.reset()
return [unparsedText]
}

As discussed, this is for parsing the sub XML tag where the queries are returned by the LLM through stream. we can move this into execute instead

vscode/src/chat/agentic/CodyTool.ts Outdated Show resolved Hide resolved
vscode/package.json Outdated Show resolved Hide resolved
vscode/src/chat/agentic/CodyTool.ts Outdated Show resolved Hide resolved
arafatkatze added a commit that referenced this pull request Jan 15, 2025
…ovider (#6650)

- Asynchronously create OpenCtxTools for model context protocol
providers
- Implement `createModelContextConfig` to handle the specific
configuration for model context protocol providers -


## Test plan

<!-- Required. See
https://docs-legacy.sourcegraph.com/dev/background-information/testing_principles.
-->

Needs to test it with the changes in
#6589

---------

Co-authored-by: arafatkatze <arafat.da.khan@gmail.com>
Copy link

‼️ Hey @sourcegraph/cody-security, please review this PR carefully as it introduces the usage of an unsafe_ function or abuses PromptString.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants