port-allocator

Automatically allocate and manage development server ports, avoiding port conflicts between multiple Claude Code instances

242 stars

Best use case

port-allocator is best used when you need a repeatable AI agent workflow instead of a one-off prompt. It is especially useful for teams working in multi. Automatically allocate and manage development server ports, avoiding port conflicts between multiple Claude Code instances

Automatically allocate and manage development server ports, avoiding port conflicts between multiple Claude Code instances

Users should expect a more consistent workflow output, faster repeated execution, and less time spent rewriting prompts from scratch.

Practical example

Example input

Use the "port-allocator" skill to help with this workflow task. Context: Automatically allocate and manage development server ports, avoiding port conflicts between multiple Claude Code instances

Example output

A structured workflow result with clearer steps, more consistent formatting, and an output that is easier to reuse in the next run.

When to use this skill

  • Use this skill when you want a reusable workflow rather than writing the same prompt again and again.

When not to use this skill

  • Do not use this when you only need a one-off answer and do not need a reusable workflow.
  • Do not use it if you cannot install or maintain the related files, repository context, or supporting tools.

Installation

Claude Code / Cursor / Codex

$curl -o ~/.claude/skills/port-allocator/SKILL.md --create-dirs "https://raw.githubusercontent.com/aiskillstore/marketplace/main/skills/guo-yu/port-allocator/SKILL.md"

Manual Installation

  1. Download SKILL.md from GitHub
  2. Place it in .claude/skills/port-allocator/SKILL.md inside your project
  3. Restart your AI agent — it will auto-discover the skill

How port-allocator Compares

Feature / Agentport-allocatorStandard Approach
Platform SupportNot specifiedLimited / Varies
Context Awareness High Baseline
Installation ComplexityUnknownN/A

Frequently Asked Questions

What does this skill do?

Automatically allocate and manage development server ports, avoiding port conflicts between multiple Claude Code instances

Where can I find the source code?

You can find the source code on GitHub using the link provided at the top of the page.

SKILL.md Source

# Port Allocator

Smart port allocator that only assigns ports to real projects containing `package.json`.

## Usage

| Command | Description |
|---------|-------------|
| `/port-allocator` | Allocate/query port for current project |
| `/port-allocator list` | List all allocated ports |
| `/port-allocator scan` | Scan code directory, discover and allocate ports for new projects |
| `/port-allocator config <path>` | Set the main code directory path |
| `/port-allocator add <dir-path>` | Manually add port allocation for a project |
| `/port-allocator allow` | Configure Claude Code permissions for this skill's commands |

## Important Rules

### 1. Only Operate on Current Project's Ports When Restarting Services

When restarting the development server, **only kill processes within the current project's port range**, never affect other ports:

```bash
# Correct: Only kill current project ports (e.g., 3000-3009)
lsof -ti:3000 | xargs kill -9 2>/dev/null
lsof -ti:3001 | xargs kill -9 2>/dev/null

# Wrong: Kill all node processes or other ports
pkill -f node  # Will affect other projects!
lsof -ti:3010 | xargs kill  # This is another project's port!
```

### 2. Append Rather Than Overwrite When Updating CLAUDE.md

When updating `~/.claude/CLAUDE.md`, **must preserve the user's existing content**:

```bash
# Correct: Check and append or update specific sections
# Wrong: Directly overwrite the entire file
```

## Execution Steps

### Command: `/port-allocator allow`

Configure Claude Code to allow commands used by this skill, avoiding manual confirmation each time:

1. Read `~/.claude/settings.json` (if exists)
2. Merge the following commands into `permissions.allow` array (preserve existing config):

```json
{
  "permissions": {
    "allow": [
      "Bash(ls -d *)",
      "Bash(find * -maxdepth * -name package.json *)",
      "Bash(cat ~/.claude/*)",
      "Bash(dirname *)",
      "Bash(lsof -i:3*)",
      "Bash(lsof -ti:3*)"
    ]
  }
}
```

3. Write updated settings.json
4. Output the list of added permissions

**Output Format:**
```
Configured Claude Code permissions

Added allowed command patterns:
  - Bash(ls -d *)
  - Bash(find * -maxdepth * -name package.json *)
  - Bash(cat ~/.claude/*)
  - Bash(lsof -i:3*)
  - Bash(lsof -ti:3*)

Config file: ~/.claude/settings.json
```

### Command: `/port-allocator config <path>`

Set the user's main code directory:

1. **Verify path exists** (required! Error and exit if not found)
2. Update `code_root` field in `~/.claude/port-registry.json`
3. Output confirmation

### First Run: Auto-Detection

On first run (when `~/.claude/port-registry.json` doesn't exist or has no `code_root`), automatically detect the code directory:

```bash
# Check common code directories
for dir in ~/Codes ~/Code ~/Projects ~/Dev ~/Development ~/repos; do
  if [ -d "$dir" ]; then
    CODE_ROOT="$dir"
    break
  fi
done

# If none exist, default to ~/Codes
CODE_ROOT="${CODE_ROOT:-~/Codes}"
```

**Auto-detection output:**
```
First run, detecting code directory...

Code directory detected: ~/Codes

Port registry initialized: ~/.claude/port-registry.json

To change, use:
   /port-allocator config ~/your/code/path
```

**If no directory found:**
```
Could not auto-detect code directory.

Please configure manually:
   /port-allocator config ~/your/code/path

Common locations:
   ~/Codes, ~/Code, ~/Projects, ~/Dev
```

### Command: `/port-allocator scan`

Scan code directory, automatically discover and register projects:

1. Read `~/.claude/port-registry.json` to get `code_root`
   - If config doesn't exist, run auto-detection first
   - If `code_root` directory doesn't exist, prompt user to configure
2. Find all directories containing `package.json` (exact to package.json location):

```bash
# Find all package.json, exclude build artifact directories
find <code_root> -maxdepth 3 -name "package.json" -type f \
  -not -path "*/.next/*" \
  -not -path "*/node_modules/*" \
  -not -path "*/dist/*" \
  -not -path "*/build/*" | while read pkg; do
  dirname "$pkg"
done
```

3. **Important**: Path must be exact to the directory containing `package.json`
   - Correct: `~/Codes/chekusu/landing`
   - Wrong: `~/Codes/chekusu` (if package.json is in subdirectory)

4. For each discovered project directory:
   - Check if already in registry
   - If not, allocate next available port range
5. Update config file (**append mode**, don't overwrite user content)
6. Output scan result summary

### Command: `/port-allocator` (default)

Allocate/query port for current project:

1. Get current working directory
2. Read config to get `code_root` and allocated ports
3. Match current directory to corresponding project
4. If no `package.json`, indicate this is not a project needing ports
5. If exists, check if port already allocated, auto-allocate if not
6. Output port info

### Command: `/port-allocator list`

List all allocated ports (read-only operation).

## Output Format

### Port Information
```
Project directory: ~/Codes/chekusu/landing
package.json: Detected
Port range: 3000-3009
   - Main app: 3000
   - API: 3001
   - Other services: 3002-3009

Warning: Only operate on ports 3000-3009 when restarting services!
```

### Scan Results
```
Scan complete: ~/Codes

Registered projects (N):
   - chekusu/landing: 3000-3009
   - saifuri: 3010-3019

Newly discovered projects (M):
   - new-project: 3090-3099 (newly allocated)

Skipped (K):
   - .next, node_modules (build artifacts)
   - research-folder (no package.json)
```

## Port Allocation Rules

- Each project is allocated **10 consecutive ports**
- Starting port: 3000
- Interval: 10
- `x0`: Main application (e.g., 3000, 3010, 3020)
- `x1`: API service (e.g., 3001, 3011, 3021)
- `x2-x9`: Other services (database, cache, etc.)

## Configuration Files

- **Port registry**: `~/.claude/port-registry.json`
- **Global instructions**: `~/.claude/CLAUDE.md` (append mode updates)
- **Claude Code settings**: `~/.claude/settings.json` (stores allowedCommands)
- **Skip patterns**: `.next`, `node_modules`, `dist`, `build`

## Notes

1. **Only operate on project ports** - Never affect other projects when restarting services
2. **Append not overwrite** - Preserve user's existing content when updating config files
3. **Precise paths** - Point to actual directory containing package.json
4. **Skip build artifacts** - .next, node_modules, etc. don't get port allocation
5. **First use** - Recommend running `/port-allocator allow` to configure permissions first

Related Skills

weekly-report

242
from aiskillstore/marketplace

帮助用户梳理周报,按照完整逻辑展示工作价值和边界。当用户说"写周报"、"周报"、"梳理周报"、"整理工作"时触发。

startup-business-analyst-market-opportunity

242
from aiskillstore/marketplace

Generate comprehensive market opportunity analysis with TAM/SAM/SOM calculations

interactive-portfolio

242
from aiskillstore/marketplace

Expert in building portfolios that actually land jobs and clients - not just showing work, but creating memorable experiences. Covers developer portfolios, designer portfolios, creative portfolios, and portfolios that convert visitors into opportunities. Use when: portfolio, personal website, showcase work, developer portfolio, designer portfolio.

daily-news-report

242
from aiskillstore/marketplace

Scrapes content based on a preset URL list, filters high-quality technical information, and generates daily Markdown reports.

customer-support

242
from aiskillstore/marketplace

Elite AI-powered customer support specialist mastering conversational AI, automated ticketing, sentiment analysis, and omnichannel support experiences. Integrates modern support tools, chatbot platforms, and CX optimization with 2024/2025 best practices. Use PROACTIVELY for comprehensive customer experience management.

azure-monitor-opentelemetry-exporter-py

242
from aiskillstore/marketplace

Azure Monitor OpenTelemetry Exporter for Python. Use for low-level OpenTelemetry export to Application Insights. Triggers: "azure-monitor-opentelemetry-exporter", "AzureMonitorTraceExporter", "AzureMonitorMetricExporter", "AzureMonitorLogExporter".

add-uint-support

242
from aiskillstore/marketplace

Add unsigned integer (uint) type support to PyTorch operators by updating AT_DISPATCH macros. Use when adding support for uint16, uint32, uint64 types to operators, kernels, or when user mentions enabling unsigned types, barebones unsigned types, or uint support.

lark-workflow-standup-report

242
from aiskillstore/marketplace

日程待办摘要:编排 calendar +agenda 和 task +get-my-tasks,生成指定日期的日程与未完成任务摘要。适用于了解今天/明天/本周的安排。

reporting-sprints

242
from aiskillstore/marketplace

Use this skill when you need to report on a sprint

reporting-issues

242
from aiskillstore/marketplace

Use this skill when you need to report on a troubleshooting session

port-adapter-designer

242
from aiskillstore/marketplace

Helps design port traits and adapter implementations for external dependencies. Activates when users need to abstract away databases, APIs, or other external systems.

mcp-transport-guide

242
from aiskillstore/marketplace

Understand MCP transport mechanisms - stdio, SSE, HTTP streaming, and custom transports