|
| 1 | +# MCP Server Development Guide |
| 2 | + |
| 3 | +This guide walks you through building your own MCP server with Python and the FastMCP framework, then connecting it to the Nexent platform. |
| 4 | + |
| 5 | +## 🌐 Language Support |
| 6 | + |
| 7 | +The MCP protocol provides SDKs for multiple programming languages: |
| 8 | + |
| 9 | +- **Python** ⭐ (recommended) |
| 10 | +- **TypeScript** |
| 11 | +- **Java** |
| 12 | +- **Go** |
| 13 | +- **Rust** |
| 14 | +- Any other language that implements the MCP protocol |
| 15 | + |
| 16 | +### Why Do We Recommend Python? |
| 17 | + |
| 18 | +We use **Python** for the examples in this guide because it offers: |
| 19 | + |
| 20 | +- ✅ **Beginner-friendly syntax**: concise code that is easy to read |
| 21 | +- ✅ **Rich ecosystem**: frameworks like FastMCP remove most boilerplate |
| 22 | +- ✅ **Rapid prototyping**: you can stand up a working server in minutes |
| 23 | +- ✅ **Mature libraries**: thousands of third-party packages are available |
| 24 | + |
| 25 | +If you are already comfortable in another language, feel free to use the corresponding MCP SDK. For a first MCP server, however, Python gives you the smoothest experience. |
| 26 | + |
| 27 | +## 📋 Prerequisites |
| 28 | + |
| 29 | +Install FastMCP before you start coding: |
| 30 | + |
| 31 | +```bash |
| 32 | +pip install fastmcp |
| 33 | +``` |
| 34 | + |
| 35 | +## 🚀 Quick Start |
| 36 | + |
| 37 | +### Minimal Example |
| 38 | + |
| 39 | +The snippet below creates a simple string utility server with FastMCP: |
| 40 | + |
| 41 | +```python |
| 42 | +from fastmcp import FastMCP |
| 43 | + |
| 44 | +# Create an MCP server instance |
| 45 | +mcp = FastMCP(name="String MCP Server") |
| 46 | + |
| 47 | +@mcp.tool( |
| 48 | + name="calculate_string_length", |
| 49 | + description="Calculate the length of a string" |
| 50 | +) |
| 51 | +def calculate_string_length(text: str) -> int: |
| 52 | + return len(text) |
| 53 | + |
| 54 | +@mcp.tool( |
| 55 | + name="to_uppercase", |
| 56 | + description="Convert text to uppercase" |
| 57 | +) |
| 58 | +def to_uppercase(text: str) -> str: |
| 59 | + return text.upper() |
| 60 | + |
| 61 | +@mcp.tool( |
| 62 | + name="to_lowercase", |
| 63 | + description="Convert text to lowercase" |
| 64 | +) |
| 65 | +def to_lowercase(text: str) -> str: |
| 66 | + return text.lower() |
| 67 | + |
| 68 | +if __name__ == "__main__": |
| 69 | + # Start with SSE transport |
| 70 | + mcp.run(transport="sse", port=8000) |
| 71 | +``` |
| 72 | + |
| 73 | +### Run the Server |
| 74 | + |
| 75 | +Save the code as `mcp_server.py` and execute: |
| 76 | + |
| 77 | +```bash |
| 78 | +python mcp_server.py |
| 79 | +``` |
| 80 | + |
| 81 | +You should see the server start successfully with the endpoint `http://127.0.0.1:8000/sse`. |
| 82 | + |
| 83 | +## 🔌 Integrate MCP Services with Nexent |
| 84 | + |
| 85 | +Once your MCP server is up, connect it to Nexent: |
| 86 | + |
| 87 | +### Step 1: Start the MCP Server |
| 88 | + |
| 89 | +Keep the server process running and note the public endpoint (for example `http://127.0.0.1:8000/sse`). |
| 90 | + |
| 91 | +### Step 2: Register the MCP Service in Nexent |
| 92 | + |
| 93 | +1. Open the **[Agent Development](../user-guide/agent-development.md)** page. |
| 94 | +2. On the “Select Agent Tools” tab, click **MCP Configuration** on the right. |
| 95 | +3. Enter the server name and server URL. |
| 96 | + - ⚠️ **Important**: |
| 97 | + 1. The server name must contain only letters and digits—no spaces or other symbols. |
| 98 | + 2. When Nexent runs inside Docker and the MCP server runs on the host, replace `127.0.0.1` with `host.docker.internal`, for example `http://host.docker.internal:8000`. |
| 99 | +4. Click **Add** to finish the registration. |
| 100 | + |
| 101 | +### Step 3: Use the MCP Tool |
| 102 | + |
| 103 | +During agent creation or editing, the newly registered MCP tool appears in the tool list and can be attached to any agent. |
| 104 | + |
| 105 | +## 🔧 Wrap Existing Workloads |
| 106 | + |
| 107 | +To expose existing business logic as MCP tools, call your internal APIs or libraries inside the tool functions. |
| 108 | + |
| 109 | +### Example: Wrap a REST API |
| 110 | + |
| 111 | +```python |
| 112 | +from fastmcp import FastMCP |
| 113 | +import requests |
| 114 | + |
| 115 | +# Create an MCP server instance |
| 116 | +mcp = FastMCP("Course Statistics Server") |
| 117 | + |
| 118 | +@mcp.tool( |
| 119 | + name="get_course_statistics", |
| 120 | + description="Get course statistics such as average, max, min, and total students" |
| 121 | +) |
| 122 | +def get_course_statistics(course_id: str) -> str: |
| 123 | + api_url = "https://your-school-api.com/api/courses/statistics" |
| 124 | + response = requests.get(api_url, params={"course_id": course_id}) |
| 125 | + |
| 126 | + if response.status_code == 200: |
| 127 | + data = response.json() |
| 128 | + stats = data.get("statistics", {}) |
| 129 | + return ( |
| 130 | + f"Course {course_id} statistics:\n" |
| 131 | + f"Average: {stats.get('average', 'N/A')}\n" |
| 132 | + f"Max: {stats.get('max', 'N/A')}\n" |
| 133 | + f"Min: {stats.get('min', 'N/A')}\n" |
| 134 | + f"Total Students: {stats.get('total_students', 'N/A')}" |
| 135 | + ) |
| 136 | + return f"API request failed: {response.status_code}" |
| 137 | + |
| 138 | +if __name__ == "__main__": |
| 139 | + # Start with SSE transport |
| 140 | + mcp.run(transport="sse", port=8000) |
| 141 | +``` |
| 142 | + |
| 143 | +### Example: Wrap an Internal Module |
| 144 | + |
| 145 | +```python |
| 146 | +from fastmcp import FastMCP |
| 147 | +from your_school_module import query_course_statistics |
| 148 | + |
| 149 | +# Create an MCP server instance |
| 150 | +mcp = FastMCP("Course Statistics Server") |
| 151 | + |
| 152 | +@mcp.tool( |
| 153 | + name="get_course_statistics", |
| 154 | + description="Get course statistics such as average, max, min, and total students" |
| 155 | +) |
| 156 | +def get_course_statistics(course_id: str) -> str: |
| 157 | + try: |
| 158 | + stats = query_course_statistics(course_id) |
| 159 | + return ( |
| 160 | + f"Course {course_id} statistics:\n" |
| 161 | + f"Average: {stats.get('average', 'N/A')}\n" |
| 162 | + f"Max: {stats.get('max', 'N/A')}\n" |
| 163 | + f"Min: {stats.get('min', 'N/A')}\n" |
| 164 | + f"Total Students: {stats.get('total_students', 'N/A')}" |
| 165 | + ) |
| 166 | + except Exception as exc: |
| 167 | + return f"Failed to query statistics: {exc}" |
| 168 | + |
| 169 | +if __name__ == "__main__": |
| 170 | + # Start with SSE transport |
| 171 | + mcp.run(transport="sse", port=8000) |
| 172 | +``` |
| 173 | + |
| 174 | +## 📚 Additional Resources |
| 175 | + |
| 176 | +### Python |
| 177 | + |
| 178 | +- [FastMCP Documentation](https://github.com/modelcontextprotocol/python-sdk) (used throughout this guide) |
| 179 | + |
| 180 | +### Other Languages |
| 181 | + |
| 182 | +- [MCP TypeScript SDK](https://github.com/modelcontextprotocol/typescript-sdk) |
| 183 | +- [MCP Java SDK](https://github.com/modelcontextprotocol/java-sdk) |
| 184 | +- [MCP Go SDK](https://github.com/modelcontextprotocol/go-sdk) |
| 185 | +- [MCP Rust SDK](https://github.com/modelcontextprotocol/rust-sdk) |
| 186 | + |
| 187 | +### General References |
| 188 | + |
| 189 | +- [MCP Protocol Specification](https://modelcontextprotocol.io/) |
| 190 | +- [Nexent Agent Development Guide](../user-guide/agent-development.md) |
| 191 | +- [MCP Tool Ecosystem Overview](./overview.md) |
| 192 | + |
| 193 | +## 🆘 Need Help? |
| 194 | + |
| 195 | +If you run into issues while developing MCP servers: |
| 196 | + |
| 197 | +1. Check the **[FAQ](../getting-started/faq.md)** |
| 198 | +2. Ask questions in [GitHub Discussions](https://github.com/ModelEngine-Group/nexent/discussions) |
| 199 | +3. Review sample servers on the [ModelScope MCP Marketplace](https://www.modelscope.cn/mcp) |
| 200 | + |
0 commit comments