-
Notifications
You must be signed in to change notification settings - Fork 755
feat: model context protocol example #546
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
19 commits
Select commit
Hold shift + click to select a range
d52aa61
feat: initial commit
CarsonRoscoe 0368aca
feat: working claude example
CarsonRoscoe bff052d
chore: removed wallet read/writes
CarsonRoscoe 15b47de
chore: updated README
CarsonRoscoe c99dc52
chore: added examples to parent READMEs
CarsonRoscoe 62af4c2
chore: format/lint
CarsonRoscoe 04cd74c
chore: added build to ignore
CarsonRoscoe 1c5ec96
fix: package dependencies
CarsonRoscoe ddbbbde
chore: change build folder to dist
CarsonRoscoe 0cff345
chore: package-lock
CarsonRoscoe 57ad17a
chore: fixed package-lock.json
CarsonRoscoe 1c242da
chore: removed dotenv dependency
CarsonRoscoe 7a73d4e
chore: removed moduleResolution from tsconfig
CarsonRoscoe 93cde16
fix: package-lock
CarsonRoscoe 2ef2ab0
pr feedback
CarsonRoscoe c76b23d
chore: lint format
CarsonRoscoe fe7e0ee
fix: package-lock
CarsonRoscoe bdc809a
fix: claud config arg for server location
CarsonRoscoe dd7f1de
feat: pr feedback
CarsonRoscoe 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
4 changes: 4 additions & 0 deletions
4
typescript/examples/model-context-protocol-cdp-server/.eslintrc.json
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,4 @@ | ||
| { | ||
| "parser": "@typescript-eslint/parser", | ||
| "extends": ["../../.eslintrc.base.json"] | ||
| } |
8 changes: 8 additions & 0 deletions
8
typescript/examples/model-context-protocol-cdp-server/.prettierignore
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,8 @@ | ||
| docs/ | ||
| dist/ | ||
| build/ | ||
| coverage/ | ||
| .github/ | ||
| src/client | ||
| **/**/*.json | ||
| *.md |
11 changes: 11 additions & 0 deletions
11
typescript/examples/model-context-protocol-cdp-server/.prettierrc
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,11 @@ | ||
| { | ||
| "tabWidth": 2, | ||
| "useTabs": false, | ||
| "semi": true, | ||
| "singleQuote": false, | ||
| "trailingComma": "all", | ||
| "bracketSpacing": true, | ||
| "arrowParens": "avoid", | ||
| "printWidth": 100, | ||
| "proseWrap": "never" | ||
| } |
59 changes: 59 additions & 0 deletions
59
typescript/examples/model-context-protocol-cdp-server/README.md
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,59 @@ | ||
| # CDP AgentKit Model Context Protocol Examples - Chatbot Typescript | ||
|
|
||
| This example demonstrates an agent setup as a server for Model Context Protocol, allowing Claude Desktop access to the full set of CDP AgentKit actions. | ||
|
|
||
| ## Ask the chatbot to engage in the Web3 ecosystem! | ||
|
|
||
| - "Transfer a portion of your ETH to a random address" | ||
| - "What is the price of BTC?" | ||
| - "Deploy an NFT that will go super viral!" | ||
| - "Deploy an ERC-20 token with total supply 1 billion" | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| ### Checking Node Version | ||
|
|
||
| Before using the example, ensure that you have the correct version of Node.js installed. The example requires Node.js 18 or higher. You can check your Node version by running: | ||
|
|
||
| ```bash | ||
| node --version | ||
| ``` | ||
|
|
||
| If you don't have the correct version, you can install it using [nvm](https://github.com/nvm-sh/nvm): | ||
|
|
||
| ```bash | ||
| nvm install node | ||
| ``` | ||
|
|
||
| This will automatically install and use the latest version of Node. | ||
|
|
||
| ### API Keys | ||
|
|
||
| You'll need the following API key: | ||
| - [CDP API Key](https://portal.cdp.coinbase.com/access/api) | ||
|
|
||
| You'll need to configure the Claude desktop config file with your CDP API keys. Copy the contents from `claude_desktop_config.json` to your Claude desktop config file and update the following: | ||
|
|
||
| 1. Update the `args` path to match the location of your built index.js file | ||
| 2. Set your CDP API keys in the `env` section: | ||
| - "CDP_API_KEY_NAME" | ||
| - "CDP_API_KEY_PRIVATE_KEY" | ||
|
|
||
| Then, navigate to the `claude_desktop_config.json` file found in your Claude Desktop apps' settings and update it's contents to the contents of our `claude_desktop_config.json` file. | ||
|
|
||
| ## Running the example | ||
|
|
||
| From the root directory, run: | ||
|
|
||
| ```bash | ||
| npm install | ||
| npm run build | ||
| ``` | ||
|
|
||
| This will install the dependencies and build the packages locally. The chatbot example uses the local `@coinbase/agentkit-model-context-protocol` and `@coinbase/agentkit` packages. If you make changes to the packages, you can run `npm run build` from root again to rebuild the packages, and your changes will be reflected in the chatbot example. | ||
|
|
||
| To use the chatbot, simply open Claude desktop after configuring your API keys. The MCP server will run automatically when you interact with Claude. | ||
|
|
||
| ## License | ||
|
|
||
| Apache-2.0 | ||
14 changes: 14 additions & 0 deletions
14
typescript/examples/model-context-protocol-cdp-server/claude_desktop_config.json
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,14 @@ | ||
| { | ||
| "mcpServers": { | ||
| "agentkit": { | ||
| "command": "node", | ||
| "args": [ | ||
| "/Users/YOUR_USER/YOUR_DIRECTORIES/agentkit/typescript/examples/model-context-protocol-cdp-server/dist/index.js" | ||
| ], | ||
| "env": { | ||
| "CDP_API_KEY_NAME": "YOUR_CDP_API_KEY_NAME", | ||
| "CDP_API_KEY_PRIVATE_KEY": "YOUR_CDP_API_KEY_PRIVATE_KEY" | ||
| } | ||
| } | ||
| } | ||
| } |
136 changes: 136 additions & 0 deletions
136
typescript/examples/model-context-protocol-cdp-server/index.ts
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,136 @@ | ||
| import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"; | ||
| import { getMcpTools } from "@coinbase/agentkit-model-context-protocol"; | ||
| import { | ||
| AgentKit, | ||
| CdpWalletProvider, | ||
| wethActionProvider, | ||
| walletActionProvider, | ||
| erc20ActionProvider, | ||
| erc721ActionProvider, | ||
| cdpApiActionProvider, | ||
| cdpWalletActionProvider, | ||
| pythActionProvider, | ||
| } from "@coinbase/agentkit"; | ||
| import { Server } from "@modelcontextprotocol/sdk/server/index.js"; | ||
| import { CallToolRequestSchema, ListToolsRequestSchema } from "@modelcontextprotocol/sdk/types.js"; | ||
|
|
||
| /** | ||
| * Validates that required environment variables are set | ||
| */ | ||
| function validateEnvironment(): void { | ||
| const requiredVars = ["CDP_API_KEY_NAME", "CDP_API_KEY_PRIVATE_KEY"]; | ||
| const missingVars = requiredVars.filter(varName => !process.env[varName]); | ||
|
|
||
| if (missingVars.length > 0) { | ||
| console.error("Error: Required environment variables are not set:"); | ||
| missingVars.forEach(varName => { | ||
| console.error(`${varName}=your_${varName.toLowerCase()}_here`); | ||
| }); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| if (!process.env.NETWORK_ID) { | ||
| console.warn("Warning: NETWORK_ID not set, defaulting to base-sepolia testnet"); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * This function creates a new server instance with the capabilities to handle MCP requests. | ||
| * It configures the CDP Wallet Provider with the provided API keys and network ID. | ||
| * It then initializes the AgentKit with the configured wallet provider and action providers. | ||
| * | ||
| * @returns {Promise<Server>} The initialized MCP server | ||
| */ | ||
| async function initializeServer() { | ||
| try { | ||
| // Create server instance with capabilities | ||
| const server = new Server( | ||
| { | ||
| name: "cdp-agentkit", | ||
| version: "1.0.0", | ||
| }, | ||
| { | ||
| capabilities: { | ||
| tools: {}, | ||
| }, | ||
| }, | ||
| ); | ||
|
|
||
| // Configure CDP Wallet Provider | ||
| const config = { | ||
| apiKeyName: process.env.CDP_API_KEY_NAME!, | ||
| apiKeyPrivateKey: process.env.CDP_API_KEY_PRIVATE_KEY!, | ||
| networkId: process.env.NETWORK_ID || "base-sepolia", | ||
| }; | ||
|
|
||
| const walletProvider = await CdpWalletProvider.configureWithWallet(config); | ||
|
|
||
| // Initialize AgentKit | ||
| const agentkit = await AgentKit.from({ | ||
| walletProvider, | ||
| actionProviders: [ | ||
| wethActionProvider(), | ||
| pythActionProvider(), | ||
| walletActionProvider(), | ||
| erc20ActionProvider(), | ||
| erc721ActionProvider(), | ||
| cdpApiActionProvider({ | ||
| apiKeyName: config.apiKeyName, | ||
| apiKeyPrivateKey: config.apiKeyPrivateKey, | ||
| }), | ||
| cdpWalletActionProvider({ | ||
| apiKeyName: config.apiKeyName, | ||
| apiKeyPrivateKey: config.apiKeyPrivateKey, | ||
| }), | ||
| ], | ||
| }); | ||
|
|
||
| // Get MCP tools from AgentKit | ||
| const { tools, toolHandler } = await getMcpTools(agentkit); | ||
|
|
||
| // Set up request handlers | ||
| server.setRequestHandler(ListToolsRequestSchema, async () => { | ||
| return { | ||
| tools, | ||
| }; | ||
| }); | ||
|
|
||
| server.setRequestHandler(CallToolRequestSchema, async request => { | ||
| try { | ||
| return await toolHandler(request.params.name, request.params.arguments); | ||
| } catch (error) { | ||
| console.error(`Error executing tool ${request.params.name}:`, error); | ||
| throw new Error(`Tool ${request.params.name} failed: ${error}`); | ||
| } | ||
| }); | ||
|
|
||
| return server; | ||
| } catch (error) { | ||
| console.error("Failed to initialize server:", error); | ||
| throw error; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Main function to run the MCP server | ||
| */ | ||
| async function main() { | ||
| validateEnvironment(); | ||
|
|
||
| try { | ||
| const server = await initializeServer(); | ||
| const transport = new StdioServerTransport(); | ||
| await server.connect(transport); | ||
| console.error("CDP AgentKit MCP Server running on stdio"); | ||
| } catch (error) { | ||
| console.error("Fatal error:", error); | ||
| process.exit(1); | ||
| } | ||
| } | ||
|
|
||
| if (require.main === module) { | ||
| main().catch(error => { | ||
| console.error("Fatal error in main():", error); | ||
| process.exit(1); | ||
| }); | ||
| } |
28 changes: 28 additions & 0 deletions
28
typescript/examples/model-context-protocol-cdp-server/package.json
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,28 @@ | ||
| { | ||
| "name": "@coinbase/cdp-model-context-protocol-server", | ||
| "description": "Coinbase AgentKit Example Model Context Protocol Server", | ||
| "version": "1.0.0", | ||
| "private": true, | ||
| "author": "Coinbase Inc.", | ||
| "license": "Apache-2.0", | ||
| "bin": { | ||
| "agentkit": "./dist/index.js" | ||
| }, | ||
| "scripts": { | ||
| "build": "tsc && node -e \"require('fs').chmodSync('dist/index.js', '755')\"", | ||
| "lint": "eslint -c .eslintrc.json *.ts", | ||
| "lint:fix": "eslint -c .eslintrc.json *.ts --fix", | ||
| "format": "prettier --write \"**/*.{ts,js,cjs,json,md}\"", | ||
| "format:check": "prettier -c .prettierrc --check \"**/*.{ts,js,cjs,json,md}\"" | ||
| }, | ||
| "dependencies": { | ||
| "@coinbase/agentkit": "^0.2.3", | ||
| "@coinbase/agentkit-model-context-protocol": "^0.1.0", | ||
| "@modelcontextprotocol/sdk": "^1.6.1", | ||
| "zod": "^3.22.4" | ||
| }, | ||
| "devDependencies": { | ||
| "nodemon": "^3.1.0", | ||
| "ts-node": "^10.9.2" | ||
| } | ||
| } |
10 changes: 10 additions & 0 deletions
10
typescript/examples/model-context-protocol-cdp-server/tsconfig.json
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,10 @@ | ||
| { | ||
| "extends": "../../tsconfig.base.json", | ||
| "compilerOptions": { | ||
| "preserveSymlinks": true, | ||
| "outDir": "./dist", | ||
| "rootDir": ".", | ||
| "module": "Node16" | ||
| }, | ||
| "include": ["*.ts"] | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.