EdgeMind-MCP-Story

EdgeMind-MCP-Story

An MCP server that connects an LLM client to a local LM Studio instance to generate and save creative writing stories as text files.

Category
访问服务器

README

EdgeMind-MCP-Story

This project is part of the Day 2 task in the course "LLMs in Edge Computing". It provides a beginner-friendly Model Context Protocol (MCP) server that connects an LLM client to a local instance of LM Studio to generate and save creative writing stories.


1. Project Overview

EdgeMind-MCP-Story is a Python-based MCP server that exposes a custom tool, generate_story. When called by an MCP-compatible client, this tool takes a story topic and optional filename, constructs a creative writing prompt, sends it to a locally running model inside LM Studio, and automatically saves the generated story as a .txt file inside a local stories/ directory.

This allows you to leverage powerful, offline language models running on edge devices (like your local PC) directly within MCP-compatible developer tools, assistants, and IDE integrations.


2. Architecture

Below is the execution flow of how the model context protocol, the client, and the local server interact:

User (Input Topic & Filename)
↓
MCP Client (e.g., Claude Desktop, Inspector, Cursor)
↓
MCP Server (server.py via stdio)
↓
LM Studio Local API (http://localhost:1234/v1/chat/completions)
↓
Local Language Model (running on your PC)
↓
Generated Story (returned to Server)
↓
stories/story.txt (written locally in UTF-8)

3. Requirements

To run this project, make sure you have:

  • Operating System: Windows 10/11
  • Python: Python 3.10 or higher installed (with pip and virtual environment support)
  • LM Studio: A local installer downloaded and configured on your PC
  • Local Language Model: A downloaded GGUF chat or instruct model inside LM Studio (e.g., Llama-3-8B-Instruct, Qwen-2.5-7B-Instruct, Phi-3-mini, etc.)
  • MCP Host/Client: An MCP-compatible client (such as Claude Desktop, Cursor, or the MCP Inspector tool)

4. Installing Dependencies

Follow these steps to set up a clean Python virtual environment and install the required dependencies:

  1. Open PowerShell or Command Prompt in the project directory:

    cd c:\Users\koppa\Desktop\EdgeMind-MCP-Story
    
  2. Create a virtual environment:

    python -m venv .venv
    
  3. Activate the virtual environment:

    • On Windows (Command Prompt):
      .venv\Scripts\activate
      
    • On Windows (PowerShell):
      .venv\Scripts\Activate.ps1
      
  4. Install the required packages:

    pip install -r requirements.txt
    

5. Setting Up LM Studio

Ensure your local model server is running before attempting to use the MCP tool:

  1. Open LM Studio on your Windows PC.
  2. Go to the Search / Discover tab (magnifying glass) and download a suitable local chat/instruct model (e.g., Qwen-2.5-7B-Instruct, Llama-3-8B-Instruct).
  3. Go to the Developer / Local Server tab (the double-arrows icon <-> or server icon on the left sidebar).
  4. Load your downloaded model using the drop-down menu at the top.
  5. Check the Port configuration (default is 1234).
  6. Click the Start Server button.
  7. Verify that the console logs show Local server listening on port 1234.
  8. Important: Look at the top of the screen or in the server logs and copy the exact model identifier (e.g. lmstudio-community/Meta-Llama-3-8B-Instruct-GGUF or qwen2.5-7b-instruct). You will need this for configuration.

6. Configure Environment Variables

Before starting the server, you must specify which model the server should instruct LM Studio to run.

Option A: System Environment Variables (Windows)

Run these commands in your terminal before running the server:

  • Command Prompt:

    set LM_STUDIO_BASE_URL=http://localhost:1234/v1
    set LM_STUDIO_MODEL=<your-copied-model-identifier>
    
  • PowerShell:

    $env:LM_STUDIO_BASE_URL="http://localhost:1234/v1"
    $env:LM_STUDIO_MODEL="<your-copied-model-identifier>"
    

Option B: Local .env File (Easiest)

Create a file named .env in the project root directory (c:\Users\koppa\Desktop\EdgeMind-MCP-Story\.env) and add the following content:

LM_STUDIO_BASE_URL=http://localhost:1234/v1
LM_STUDIO_MODEL=your-copied-model-identifier

Replace your-copied-model-identifier with the exact name you copied in Section 5.


7. Running the MCP Server

You can start the MCP server directly using:

python server.py

Note: Because MCP servers use standard input/output (stdio) transport for communication with clients, running it directly in the terminal will not show a web UI or accept keyboard commands. It will wait silently for JSON-RPC messages from a client.

To stop the server, press Ctrl + C.

7.1 Running Offline Tests

To verify the logic of the server (such as filename sanitization, error responses, and mock LM Studio integration) offline, run:

python test_server.py

This runs the automated unit tests in test_server.py and ensures everything is functioning properly without needing a live LM Studio connection.


8. Connecting the MCP Server to an MCP Client

To use the tool with an actual LLM, you need to configure an MCP client to launch the server.

Example: Claude Desktop

To add this server to the Claude Desktop application, open your Claude configuration file (located at %APPDATA%\Claude\claude_desktop_config.json) and add this configuration:

{
  "mcpServers": {
    "edgemind-story-writer": {
      "command": "python",
      "args": [
        "c:/Users/koppa/Desktop/EdgeMind-MCP-Story/server.py"
      ],
      "env": {
        "LM_STUDIO_BASE_URL": "http://localhost:1234/v1",
        "LM_STUDIO_MODEL": "<your-model-identifier-here>"
      }
    }
  }
}

Note: If you configured environment variables via a .env file inside the directory, you do not need to add the "env" block in the JSON file. Be sure to restart Claude Desktop after modifying this configuration.


9. Using the Tool

When connected, the MCP client gains access to the tool generate_story.

Arguments:

  • topic (string, required): The core concept or prompt for your story.
  • filename (string, optional): A custom filename for the output file (e.g. mysterious_device.txt). If left blank, a safe filename will be automatically created based on the topic.

Example Prompt:

"Write a story about: A student discovers a mysterious device inside his college laboratory."

The client will automatically route this to the generate_story tool.


10. Output

All successfully written stories are stored inside the stories/ directory:

stories/
  └── mysterious_device.txt

The server saves files using UTF-8 encoding. The output response returned to your MCP client will contain:

  1. A confirmation that the story was generated by your local model.
  2. The exact relative path where it was saved.
  3. The topic of the story.
  4. The file size and word count details.

11. Troubleshooting

1. "Error: Could not reach LM Studio at..."

  • Cause: LM Studio is not running, or the Local Server has not been started.
  • Solution: Open LM Studio, go to the Local Server tab, and verify that the server is started and listening on the port configured in LM_STUDIO_BASE_URL (usually 1234).

2. "Error: The environment variable 'LM_STUDIO_MODEL' is not set"

  • Cause: The server doesn't know which model to query.
  • Solution: Ensure you have defined LM_STUDIO_MODEL in your terminal environment, or created a .env file with the model name in the project directory.

3. "Error: LM Studio returned HTTP status 400 (or other api error)"

  • Cause: The model name defined in LM_STUDIO_MODEL does not match the model currently loaded in LM Studio.
  • Solution: Double-check the spelling of the model identifier at the top of the LM Studio window. Set it exactly as shown.

4. "Error: Cannot create or access the 'stories' folder"

  • Cause: Python does not have write permissions to the project directory.
  • Solution: Run terminal as Administrator or verify folder permissions in Windows.

12. Project Architecture

The project contains only the files required to run, keeping it simple and easy to demonstrate:

  • server.py: The core application file. Contains the MCP server instantiation, configuration validation, the generate_story tool, filename security sanitization, HTTP request payload creation, and local file storage logic.
  • test_server.py: Automated unit tests for verifying filename sanitization, input validation, and mocked API interaction offline.
  • requirements.txt: List of Python external dependencies required to run the server.
  • stories/: Directory where all generated story text files will be written.
  • .gitignore: Tells Git to track the project files while ignoring local environment variables, Python cache directories, and generated story txt outputs.
  • README.md: This guide.

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选